-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
We need better temporary file and dir API #9372
Comments
Probably worth noting there's already a nimble package for this. https://github.com/OpenSystemsLab/tempfile.nim |
ah, thanks! but definitely some points in this issue should be addressed. in the eternal debate of 'batteries included or not', I feel temp file creation should be part of stdlib because it's generally useful AND because it's likely to be used in Nim repo itself (eg a lot of tests should use tempfile creation instead of what's being done currently: $nimc_D/tests/stdlib/tparsecfg.nim |
note: https://github.com/OpenSystemsLab/tempfile.nim has limitations, eg:
|
Python tempfile implementation: |
Ironically, nimble itself uses |
Yes, these seem to be Posix-only. I just find myself in the situation where I want to adapt a program that I developed under Linux to Windows and this |
@sschwarzer yeah ok, but |
That's interesting, thank you. I had assumed that things in the I agree with @timotheecour that creating temporary directories and files should be in the Nim standard library. I'll probably go with the external tempfile library for now that was mentioned above. I had considered working on (Regarding PHP, I haven't used it in years, although not specifically because of the temp directory omission but because I didn't have a project where I'd use PHP. And by the way, if I say that a feature should be in the standard library of a language that doesn't automatically mean I won't use the language until the standard library has the feature.) |
I'm a git/github newbie so please be gentle, but have just opened #14347 to get support for template withTempFile(prefix: string, suffix: string, code: untyped): untyped =
let (filename, tempfile {.inject.}) = mkstemp(prefix, suffix)
let tempfilename {.inject.} = absolutePath(filename)
try:
code
finally:
removeFile(tempfilename) Is the user-friendly version of the same, but I don't know if that has the cross-platform support it would need to go into |
May be https://github.com/OpenSystemsLab/tempfile.nim should be made part of fusion ( https://github.com/nim-lang/fusion ) if the author allows? /cc @OpenSystemsLab @ba0f3 |
@kaushalmodi well with MIT you don't really need permission, just need to keep the license, but yeah, it would be a nice thing to ask :) |
@kaushalmodi why not 😀 |
just create a PR against fusion ? |
* close nim-lang#9372 add std/tempfile
getTempDir
; EDIT: solved by better getTempDir #16914getTempDir
comes with a warningPlease do not use this
and indeed its implementation should be replaced (or alternatively, deprecated for another proc if backward compatibility is an issue) by something more robust.calling the Windows API function GetTempPath.
(https://docs.microsoft.com/en-us/windows/desktop/api/fileapi/nf-fileapi-gettemppatha) instead ofstring(getEnv("TEMP")) & "\\"
getHomeDir()
, https://stackoverflow.com/questions/2364740/where-is-the-temp-folder-in-android-device points to/data/local/tmp
orgetCacheDir()
There's, in posix,
proc mkstemp*(tmpl: cstring): cint
but that's posix only and too low-level (eg see accompanying warning to it)Shopping around for alternatives, python has nice standard library library with low level and high level API around temp file/dir creation, including facilities for cleanup, and with epmhasis on security (eg see description around TemporaryFile).
We could start with porting these:
Note: the D variant (https://dlang.org/library/std/stdio/file.tmpfile.html) is not as convenient as it returns a File object (and no way to access its name)
links
The text was updated successfully, but these errors were encountered: