Skip to content

Commit

Permalink
Merge pull request jruby#7973 from headius/default_tempfile
Browse files Browse the repository at this point in the history
Use gem for tempfile
  • Loading branch information
headius authored Jun 19, 2024
2 parents b9796e2 + a7de515 commit 97b07e9
Show file tree
Hide file tree
Showing 11 changed files with 81 additions and 513 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ lib/ruby/stdlib/stringio*
lib/ruby/stdlib/strscan*
lib/ruby/stdlib/subspawn*
lib/ruby/stdlib/syntax_suggest*
lib/ruby/stdlib/tempfile.rb
lib/ruby/stdlib/time.rb
lib/ruby/stdlib/timeout*
lib/ruby/stdlib/tsort*
Expand Down
23 changes: 5 additions & 18 deletions core/src/main/java/org/jruby/RubyFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -457,18 +457,9 @@ public static IRubyObject path(ThreadContext context, IRubyObject self, IRubyObj
return StringSupport.checkEmbeddedNulls(context.runtime, get_path(context, str));
}

@JRubyMethod(name = {"path", "to_path"})
// Moved to IO in 3.2
public IRubyObject path(ThreadContext context) {
if ((openFile.getMode() & OpenFile.TMPFILE) != 0) {
throw context.runtime.newIOError("File is unnamed (TMPFILE?)");
}

final String path = getPath();
if (path != null) {
RubyString newPath = context.runtime.newString(path);
return newPath;
}
return context.nil;
return super.path(context);
}

@JRubyMethod
Expand Down Expand Up @@ -1466,14 +1457,10 @@ public static IRubyObject mkfifo(ThreadContext context, RubyString path, int mod
return RubyFixnum.zero(runtime);
}

// Moved to IO in 3.2
@Override
public String getPath() {
if (openFile == null) return null;
return openFile.getPath();
}

private void setPath(String path) {
if (openFile == null) return;
openFile.setPath(path);
return super.getPath();
}

@Override
Expand Down
76 changes: 50 additions & 26 deletions core/src/main/java/org/jruby/RubyIO.java
Original file line number Diff line number Diff line change
Expand Up @@ -4812,22 +4812,24 @@ private static RubyFixnum copyStreamCommon(ThreadContext context, IRubyObject ar
boolean local2 = false;

try {
if (arg1 instanceof RubyString) {
io1 = (RubyIO) RubyFile.open(context, runtime.getFile(), new IRubyObject[]{arg1}, Block.NULL_BLOCK);
local1 = true;
} else if (arg1 instanceof RubyIO) {
if (arg1 == runtime.getArgsFile() || !(arg1 instanceof RubyFile || arg1 instanceof RubyString || arg1.respondsTo("to_path"))) {
if (sites.respond_to_read.respondsTo(context, arg1, arg1, true)) {
channel1 = new IOChannel.IOReadableByteChannel(arg1);
} else if (sites.respond_to_readpartial.respondsTo(context, arg1, arg1, true)) {
channel1 = new IOChannel.IOReadableByteChannel(arg1, "readpartial");
}
} else {
IRubyObject tmpIO = TypeConverter.ioCheckIO(runtime, arg1);
if (!tmpIO.isNil()) {
arg1 = tmpIO;
} else if (!(arg1 instanceof RubyFile)) {
RubyString path = RubyFile.get_path(context, arg1);
arg1 = RubyFile.open(context, runtime.getFile(), new IRubyObject[]{path, runtime.newFixnum(ModeFlags.RDONLY)}, Block.NULL_BLOCK);
local1 = true;
}

io1 = (RubyIO) arg1;
userProvidedReadIO = true;
} else if (sites.to_path_checked1.respond_to_X.respondsTo(context, arg1, arg1)) {
RubyString path = (RubyString) TypeConverter.convertToType(context, arg1, runtime.getString(), sites.to_path_checked1);
io1 = (RubyIO) RubyFile.open(context, runtime.getFile(), new IRubyObject[]{path}, Block.NULL_BLOCK);
local1 = true;
} else if (sites.respond_to_read.respondsTo(context, arg1, arg1, true)) {
channel1 = new IOChannel.IOReadableByteChannel(arg1);
} else if (sites.respond_to_readpartial.respondsTo(context, arg1, arg1, true)) {
channel1 = new IOChannel.IOReadableByteChannel(arg1, "readpartial");
} else {
throw runtime.newArgumentError("Should be String or IO");
}

// for instance IO, just use its channel
Expand All @@ -4836,19 +4838,21 @@ private static RubyFixnum copyStreamCommon(ThreadContext context, IRubyObject ar
channel1 = io1.getChannel();
}

if (arg2 instanceof RubyString) {
io2 = (RubyIO) RubyFile.open(context, runtime.getFile(), new IRubyObject[]{arg2, runtime.newString("w")}, Block.NULL_BLOCK);
local2 = true;
} else if (arg2 instanceof RubyIO) {
io2 = (RubyIO) arg2;
} else if (sites.to_path_checked2.respond_to_X.respondsTo(context, arg2, arg2)) {
RubyString path = (RubyString) TypeConverter.convertToType(context, arg2, runtime.getString(), sites.to_path_checked2);
io2 = (RubyIO) RubyFile.open(context, runtime.getFile(), new IRubyObject[]{path, runtime.newString("w")}, Block.NULL_BLOCK);
local2 = true;
} else if (sites.respond_to_write.respondsTo(context, arg2, arg2, true)) {
channel2 = new IOChannel.IOWritableByteChannel(arg2);
if (arg2 == runtime.getArgsFile() || !(arg2 instanceof RubyFile || arg2 instanceof RubyString || arg2.respondsTo("to_path"))) {
if (sites.respond_to_write.respondsTo(context, arg2, arg2, true)) {
channel2 = new IOChannel.IOWritableByteChannel(arg2);
}
} else {
throw runtime.newArgumentError("Should be String or IO");
IRubyObject tmpIO = TypeConverter.ioCheckIO(runtime, arg2);
if (!tmpIO.isNil()) {
arg2 = tmpIO;
} else if (!(arg2 instanceof RubyFile)) {
RubyString path = RubyFile.get_path(context, arg2);
arg2 = RubyFile.open(context, runtime.getFile(), new IRubyObject[]{path, runtime.newFixnum(ModeFlags.WRONLY | ModeFlags.CREAT | ModeFlags.TRUNC)}, Block.NULL_BLOCK);
local2 = true;
}

io2 = (RubyIO) arg2;
}

// for instanceof IO, just use its write channel
Expand Down Expand Up @@ -5194,6 +5198,26 @@ private static IRubyObject doWait(ThreadContext context, RubyIO io, OpenFile fpt
return context.nil;
}

@JRubyMethod(name = {"path", "to_path"})
public IRubyObject path(ThreadContext context) {
final String path = getPath();
if (path != null) {
RubyString newPath = context.runtime.newString(path);
return newPath;
}
return context.nil;
}

public String getPath() {
if (openFile == null) return null;
return openFile.getPath();
}

protected void setPath(String path) {
if (openFile == null) return;
openFile.setPath(path);
}

/**
* Add a thread to the list of blocking threads for this IO.
*
Expand Down
Loading

0 comments on commit 97b07e9

Please sign in to comment.