Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: jruby/jruby
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 14ae9cd4be13
Choose a base ref
...
head repository: jruby/jruby
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 1c98caacba80
Choose a head ref
  • 6 commits
  • 9 files changed
  • 1 contributor

Commits on Feb 20, 2016

  1. Copy the full SHA
    50e208b View commit details
  2. Fix error message for backtrace.

    Fixes MRI TestException#test_errat once using MRI backtraces.
    headius committed Feb 20, 2016
    Copy the full SHA
    b09fbe6 View commit details
  3. Copy the full SHA
    d1d8531 View commit details
  4. Copy the full SHA
    1cb6e4c View commit details
  5. Copy the full SHA
    35ffae1 View commit details
  6. Fixes for io/console tests from MRI.

    * Set up "GenericReadable" and "GenericWritable" modules in both
      StringIO and io/console so they can update each other.
    * IO.console should be sync.
    * IO.console should return nil with we don't have tty.
    * MRI test_io_console.rb should not sub rubyw into command line if
      it is the same already.
    * Move common impls of getch and getpass out of conditional logic
      in console.rb.
    * Implement getpass for both IO and GenericReadable.
    headius committed Feb 20, 2016
    Copy the full SHA
    1c98caa View commit details
2 changes: 1 addition & 1 deletion core/src/main/java/org/jruby/RubyException.java
Original file line number Diff line number Diff line change
@@ -99,7 +99,7 @@ private void setBacktrace(IRubyObject obj) {
} else if (obj instanceof RubyString) {
backtrace = RubyArray.newArray(getRuntime(), obj);
} else {
throw getRuntime().newTypeError("backtrace must be Array of String or a single String");
throw getRuntime().newTypeError("backtrace must be Array of String");
}
}

300 changes: 154 additions & 146 deletions core/src/main/java/org/jruby/ext/stringio/StringIO.java
Original file line number Diff line number Diff line change
@@ -38,6 +38,7 @@
import org.jruby.RubyFixnum;
import org.jruby.RubyIO;
import org.jruby.RubyKernel;
import org.jruby.RubyModule;
import org.jruby.RubyNumeric;
import org.jruby.RubyObject;
import org.jruby.RubyString;
@@ -99,6 +100,14 @@ public static RubyClass createStringIOClass(final Ruby runtime) {
stringIOClass.defineAnnotatedMethods(IOJavaAddons.AnyIO.class);
}

RubyModule genericReadable = runtime.getIO().defineOrGetModuleUnder("GenericReadable");
genericReadable.defineAnnotatedMethods(GenericReadable.class);
stringIOClass.includeModule(genericReadable);

RubyModule genericWritable = runtime.getIO().defineOrGetModuleUnder("GenericWritable");
genericWritable.defineAnnotatedMethods(GenericWritable.class);
stringIOClass.includeModule(genericWritable);

return stringIOClass;
}

@@ -236,14 +245,6 @@ public IRubyObject strioNil(ThreadContext context) {
return context.nil;
}

@JRubyMethod(name = "<<", required = 1)
public IRubyObject append(ThreadContext context, IRubyObject arg) {
// Claims conversion is done via 'to_s' in docs.
callMethod(context, "write", arg);

return this;
}

@JRubyMethod
public IRubyObject close(ThreadContext context) {
checkInitialized();
@@ -690,17 +691,6 @@ public IRubyObject set_pos(IRubyObject arg) {
return arg;
}

@JRubyMethod(name = "print", rest = true)
public IRubyObject print(ThreadContext context, IRubyObject[] args) {
return RubyIO.print(context, this, args);
}

@JRubyMethod(name = "printf", required = 1, rest = true)
public IRubyObject printf(ThreadContext context, IRubyObject[] args) {
callMethod(context, "write", RubyKernel.sprintf(context, this, args));
return getRuntime().getNil();
}

private void strioExtend(int pos, int len) {
checkModifiable();

@@ -741,63 +731,6 @@ public IRubyObject putc(ThreadContext context, IRubyObject ch) {

public static final ByteList NEWLINE = ByteList.create("\n");

@JRubyMethod(name = "puts", rest = true)
public IRubyObject puts(ThreadContext context, IRubyObject[] args) {
checkModifiable();
return puts(context, this, args);
}

private static IRubyObject puts(ThreadContext context, IRubyObject maybeIO, IRubyObject[] args) {
// TODO: This should defer to RubyIO logic, but we don't have puts right there for 1.9
Ruby runtime = context.runtime;
if (args.length == 0) {
RubyIO.write(context, maybeIO, RubyString.newStringShared(runtime, NEWLINE));
return runtime.getNil();
}

for (int i = 0; i < args.length; i++) {
RubyString line = null;

if (!args[i].isNil()) {
IRubyObject tmp = args[i].checkArrayType();
if (!tmp.isNil()) {
RubyArray arr = (RubyArray) tmp;
if (runtime.isInspecting(arr)) {
line = runtime.newString("[...]");
} else {
inspectPuts(context, maybeIO, arr);
continue;
}
} else {
if (args[i] instanceof RubyString) {
line = (RubyString) args[i];
} else {
line = args[i].asString();
}
}
}

if (line != null) RubyIO.write(context, maybeIO, line);

if (line == null || !line.getByteList().endsWith(NEWLINE)) {
RubyIO.write(context, maybeIO, RubyString.newStringShared(runtime, NEWLINE));
}
}

return runtime.getNil();
}

private static IRubyObject inspectPuts(ThreadContext context, IRubyObject maybeIO, RubyArray array) {
Ruby runtime = context.runtime;
try {
runtime.registerInspecting(array);
return puts(context, maybeIO, array.toJavaArray());
}
finally {
runtime.unregisterInspecting(array);
}
}

@JRubyMethod(name = "read", optional = 2)
public IRubyObject read(ThreadContext context, IRubyObject[] args) {
checkReadable();
@@ -870,52 +803,6 @@ public IRubyObject read(ThreadContext context, IRubyObject[] args) {
return string;
}

@JRubyMethod(name = "read_nonblock", required = 1, optional = 2)
public IRubyObject read_nonblock(ThreadContext context, IRubyObject[] args) {
Ruby runtime = context.runtime;

IRubyObject opts = ArgsUtil.getOptionsArg(runtime, args);

if (!opts.isNil()) args = Arrays.copyOf(args, args.length - 1);

boolean exception = ArgsUtil.extractKeywordArg(context, "exception", opts) != runtime.getFalse();

IRubyObject val = read(context, args);
if (val.isNil()) {
if (!exception) return context.nil;
throw context.runtime.newEOFError();
}

return val;
}

@JRubyMethod(name = "readchar")
public IRubyObject readchar(ThreadContext context) {
IRubyObject c = callMethod(context, "getc");

if (c.isNil()) throw context.runtime.newEOFError();

return c;
}

@JRubyMethod(name = "readbyte")
public IRubyObject readbyte(ThreadContext context) {
IRubyObject b = callMethod(context, "getbyte");

if (b.isNil()) throw context.runtime.newEOFError();

return b;
}

@JRubyMethod(name = "readline", optional = 1, writes = FrameField.LASTLINE)
public IRubyObject readline(ThreadContext context, IRubyObject[] args) {
IRubyObject line = callMethod(context, "gets", args);

if (line.isNil()) throw context.runtime.newEOFError();

return line;
}

@JRubyMethod(name = "readlines", optional = 2)
public IRubyObject readlines(ThreadContext context, IRubyObject[] args) {
Ruby runtime = context.runtime;
@@ -1016,18 +903,9 @@ public IRubyObject sync(ThreadContext context) {
return context.runtime.getTrue();
}

@JRubyMethod(name = {"sysread", "readpartial"}, optional = 2)
public IRubyObject sysread(ThreadContext context, IRubyObject[] args) {
IRubyObject val = callMethod(context, "read", args);

if (val.isNil()) throw getRuntime().newEOFError();

return val;
}

// only here for the fake-out class in org.jruby
public IRubyObject sysread(IRubyObject[] args) {
return sysread(getRuntime().getCurrentContext(), args);
return GenericReadable.sysread(getRuntime().getCurrentContext(), this, args);
}

@JRubyMethod(name = "truncate", required = 1)
@@ -1113,20 +991,6 @@ public IRubyObject ungetbyte(ThreadContext context, IRubyObject arg) {
return context.nil;
}

@JRubyMethod(name = "syswrite", required = 1)
public IRubyObject syswrite(ThreadContext context, IRubyObject arg) {
return RubyIO.write(context, this, arg);
}

@JRubyMethod(name = "write_nonblock", required = 1, optional = 1)
public IRubyObject syswrite_nonblock(ThreadContext context, IRubyObject[] args) {
Ruby runtime = context.runtime;

ArgsUtil.getOptionsArg(runtime, args); // ignored as in MRI

return syswrite(context, args[0]);
}

// MRI: strio_write
@JRubyMethod(name = {"write"}, required = 1)
public IRubyObject write(ThreadContext context, IRubyObject arg) {
@@ -1236,6 +1100,150 @@ public IRubyObject codepoints(ThreadContext context, Block block) {
return each_codepoint(context, block);
}

public static class GenericReadable {
@JRubyMethod(name = "readchar")
public static IRubyObject readchar(ThreadContext context, IRubyObject self) {
IRubyObject c = self.callMethod(context, "getc");

if (c.isNil()) throw context.runtime.newEOFError();

return c;
}

@JRubyMethod(name = "readbyte")
public static IRubyObject readbyte(ThreadContext context, IRubyObject self) {
IRubyObject b = self.callMethod(context, "getbyte");

if (b.isNil()) throw context.runtime.newEOFError();

return b;
}

@JRubyMethod(name = "readline", optional = 1, writes = FrameField.LASTLINE)
public static IRubyObject readline(ThreadContext context, IRubyObject self, IRubyObject[] args) {
IRubyObject line = self.callMethod(context, "gets", args);

if (line.isNil()) throw context.runtime.newEOFError();

return line;
}

@JRubyMethod(name = {"sysread", "readpartial"}, optional = 2)
public static IRubyObject sysread(ThreadContext context, IRubyObject self, IRubyObject[] args) {
IRubyObject val = self.callMethod(context, "read", args);

if (val.isNil()) throw context.runtime.newEOFError();

return val;
}

@JRubyMethod(name = "read_nonblock", required = 1, optional = 2)
public static IRubyObject read_nonblock(ThreadContext context, IRubyObject self, IRubyObject[] args) {
Ruby runtime = context.runtime;

IRubyObject opts = ArgsUtil.getOptionsArg(runtime, args);

if (!opts.isNil()) args = Arrays.copyOf(args, args.length - 1);

boolean exception = ArgsUtil.extractKeywordArg(context, "exception", opts) != runtime.getFalse();

IRubyObject val = self.callMethod(context, "read", args);
if (val.isNil()) {
if (!exception) return context.nil;
throw context.runtime.newEOFError();
}

return val;
}
}

public static class GenericWritable {
@JRubyMethod(name = "<<", required = 1)
public static IRubyObject append(ThreadContext context, IRubyObject self, IRubyObject arg) {
// Claims conversion is done via 'to_s' in docs.
self.callMethod(context, "write", arg);

return self;
}

@JRubyMethod(name = "print", rest = true)
public static IRubyObject print(ThreadContext context, IRubyObject self, IRubyObject[] args) {
return RubyIO.print(context, self, args);
}

@JRubyMethod(name = "printf", required = 1, rest = true)
public static IRubyObject printf(ThreadContext context, IRubyObject self, IRubyObject[] args) {
self.callMethod(context, "write", RubyKernel.sprintf(context, self, args));
return context.nil;
}

@JRubyMethod(name = "puts", rest = true)
public static IRubyObject puts(ThreadContext context, IRubyObject maybeIO, IRubyObject[] args) {
// TODO: This should defer to RubyIO logic, but we don't have puts right there for 1.9
Ruby runtime = context.runtime;
if (args.length == 0) {
RubyIO.write(context, maybeIO, RubyString.newStringShared(runtime, NEWLINE));
return runtime.getNil();
}

for (int i = 0; i < args.length; i++) {
RubyString line = null;

if (!args[i].isNil()) {
IRubyObject tmp = args[i].checkArrayType();
if (!tmp.isNil()) {
RubyArray arr = (RubyArray) tmp;
if (runtime.isInspecting(arr)) {
line = runtime.newString("[...]");
} else {
inspectPuts(context, maybeIO, arr);
continue;
}
} else {
if (args[i] instanceof RubyString) {
line = (RubyString) args[i];
} else {
line = args[i].asString();
}
}
}

if (line != null) RubyIO.write(context, maybeIO, line);

if (line == null || !line.getByteList().endsWith(NEWLINE)) {
RubyIO.write(context, maybeIO, RubyString.newStringShared(runtime, NEWLINE));
}
}

return runtime.getNil();
}

private static IRubyObject inspectPuts(ThreadContext context, IRubyObject maybeIO, RubyArray array) {
Ruby runtime = context.runtime;
try {
runtime.registerInspecting(array);
return puts(context, maybeIO, array.toJavaArray());
}
finally {
runtime.unregisterInspecting(array);
}
}

@JRubyMethod(name = "syswrite", required = 1)
public static IRubyObject syswrite(ThreadContext context, IRubyObject self, IRubyObject arg) {
return RubyIO.write(context, self, arg);
}

@JRubyMethod(name = "write_nonblock", required = 1, optional = 1)
public static IRubyObject syswrite_nonblock(ThreadContext context, IRubyObject self, IRubyObject[] args) {
Ruby runtime = context.runtime;

ArgsUtil.getOptionsArg(runtime, args); // ignored as in MRI

return syswrite(context, self, args[0]);
}
}

/* rb: check_modifiable */
public void checkFrozen() {
super.checkFrozen();
Loading