Skip to content

Remove Tempfile#6485

Merged
RX14 merged 1 commit into
crystal-lang:masterfrom
straight-shoota:jm/feature/remove-tempfile
Aug 31, 2018
Merged

Remove Tempfile#6485
RX14 merged 1 commit into
crystal-lang:masterfrom
straight-shoota:jm/feature/remove-tempfile

Conversation

@straight-shoota
Copy link
Copy Markdown
Member

@straight-shoota straight-shoota commented Aug 4, 2018

This removes Tempfile and places its methods in File and Dir as proposed in #5939 and standardizes the arguments as proposed in #5938. All tempfile methods now receive prefix, suffix (and optional dir) arguments. There are also overload for only one argument suffix (and optional dir) because it is pretty common to create a temp file with a specific extension.

Previously, require "tempfile" was needed in order to use this feature. Now that it is integrated into File I think it makes sense to have it in corelib.

I'm not sure if tempfile with block should perhaps actually delete the tempfile. Or if there could be some other mechanism to simplify automatic deletion of tempfiles. This could be discussed in a follow-up issue, though.
Same goes for the addition of a method to create a temporary directory (see #4096). The only issue with this is that Dir.tempdir would also be a fitting name for this but it is now used to query the tempdir environment setting.
Related to that, there could also be a future improvement to allow modifying the global tempdir, probably in a way similar to Time::Location.local.

Fixes #5939 and #5938

Comment thread src/file/tempfile.cr
# It is the caller's responsibility to remove the file when no longer needed.
def self.tempfile(prefix : String?, suffix : String?, *, dir : String = Dir.tempdir, encoding = nil, invalid = nil)
fileno, path = Crystal::System::File.mktemp(prefix, suffix, dir)
new(path, fileno, blocking: true, encoding: encoding, invalid: invalid)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why blocking true?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure... I just copied it form Tempfile#initialize.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

all files use blocking IO anyway because O_NONBLOCK has no effect on files.

@straight-shoota straight-shoota force-pushed the jm/feature/remove-tempfile branch 3 times, most recently from 83d62c8 to e511c80 Compare August 4, 2018 21:13
@ysbaddaden
Copy link
Copy Markdown
Collaborator

As said in issues about this, I don't like this breaking change. I prefer to keep concerns distinct, and really see no need to push this into corelib.

@straight-shoota straight-shoota force-pushed the jm/feature/remove-tempfile branch from e511c80 to e87ba4e Compare August 5, 2018 08:19
@straight-shoota
Copy link
Copy Markdown
Member Author

I think we established that there is little reason for Tempfile being a separate type with exactly the same behaviour as File.

An alternative option might be to keep Tempfile as a module and have these methods defined there, but return an ordinary File instance. This would reduce the breaking changes, it would just be that Tempfile.new returns File instead of Tempfile (etc.).

But the changes in the method signatures need to be breaking anyways in order to provide a sane API.

@straight-shoota straight-shoota force-pushed the jm/feature/remove-tempfile branch from e87ba4e to 089e9fe Compare August 5, 2018 09:22
@ghost ghost mentioned this pull request Aug 6, 2018
Copy link
Copy Markdown
Member

@RX14 RX14 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this change.

Comment thread src/file/tempfile.cr
# It is the caller's responsibility to remove the file when no longer needed.
def self.tempfile(prefix : String?, suffix : String?, *, dir : String = Dir.tempdir, encoding = nil, invalid = nil)
fileno, path = Crystal::System::File.mktemp(prefix, suffix, dir)
new(path, fileno, blocking: true, encoding: encoding, invalid: invalid)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

all files use blocking IO anyway because O_NONBLOCK has no effect on files.

@RX14 RX14 added kind:feature topic:stdlib breaking-change May have breaking effect in some edge cases. Mostly negligible, but still noteworthy. labels Aug 7, 2018
@straight-shoota straight-shoota force-pushed the jm/feature/remove-tempfile branch from 089e9fe to cf9eb74 Compare August 7, 2018 10:56
@straight-shoota
Copy link
Copy Markdown
Member Author

straight-shoota commented Aug 7, 2018

Rebased

@straight-shoota straight-shoota force-pushed the jm/feature/remove-tempfile branch from cf9eb74 to 400f70f Compare August 7, 2018 11:44
Copy link
Copy Markdown
Member

@sdogruyol sdogruyol left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @straight-shoota 👍

@RX14 RX14 requested a review from bcardiff August 9, 2018 19:05
@straight-shoota
Copy link
Copy Markdown
Member Author

@ysbaddaden Would you feel more comfortable if we put the tempfile methods back in Tempfile namespace (as module) and just return File?
This would separate tempfile methods from regular File methods. And it wouldn't need to be in core lib.

@RX14
Copy link
Copy Markdown
Member

RX14 commented Aug 23, 2018

I see no reason why they can't be in the corelib, it's not particularly much more code (most of the code would be required as part of crystal/system anyway) and it's only dependency is libc. It doesn't drag in anything more to the corelib. This PR is fine as-is.

Copy link
Copy Markdown
Member

@asterite asterite left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this change. It removes one type from the standard library (Tempfile) which isn't much different than a file. So I find this way simpler than the previous one.

@RX14 RX14 merged commit 57f5473 into crystal-lang:master Aug 31, 2018
@RX14 RX14 added this to the 0.27.0 milestone Aug 31, 2018
@straight-shoota straight-shoota deleted the jm/feature/remove-tempfile branch September 3, 2018 18:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

breaking-change May have breaking effect in some edge cases. Mostly negligible, but still noteworthy. kind:feature topic:stdlib

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Remove class Tempfile

5 participants