Skip to content

Commit f576b68

Browse files
committed
fix(py),fix(js): n_tempfile: mktemp raised OSError over FileNotFoundError
- fix(js): use getpid that's available when JS - impr: use tryOsOp when we can
1 parent f361005 commit f576b68

File tree

1 file changed

+5
-6
lines changed

1 file changed

+5
-6
lines changed

src/pylib/Lib/n_tempfile.nim

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import std/random
44
import std/options
55

66
import ./n_os
7+
import ../pyerrors/oserr
78

89
when defined(js): {.error: "pylib tempfile not support JS currently".}
910
import ./io
@@ -23,7 +24,7 @@ var name_sequence{.threadvar.}: RandomNameSequence
2324
const RandChars = "abcdefghijklmnopqrstuvwxyz0123456789_"
2425

2526
proc initRandomNameSequence(self: var RandomNameSequence) =
26-
let cur_pid = getCurrentProcessId()
27+
let cur_pid = n_os.getpid()
2728
if cur_pid != self.rng_pid:
2829
self.rng = initRand()
2930
self.rng_pid = cur_pid
@@ -46,7 +47,7 @@ proc mktemp*(suffix="", prefix=templ, dir = "", checker=fileExists): string =
4647
let file = dir / prefix & name & suffix
4748
if not checker file:
4849
return file
49-
raise newException(OSError, "No usable temporary filename found")
50+
raise newException(FileNotFoundError, "No usable temporary filename found")
5051

5152

5253
proc candidate_tempdir_list(): seq[string] =
@@ -279,11 +280,9 @@ proc TemporaryDirectory*(suffix=sNone, prefix=sNone, dir=sNone, ignore_cleanup_e
279280

280281
proc cleanup*(self: TemporaryDirectoryWrapper) =
281282
if self.delete:
282-
try:
283+
tryOsOp not self.ignore_cleanup_errors:
283284
removeDir self.name
284-
except OSError:
285-
if not self.ignore_cleanup_errors:
286-
raise # XXX: see Py's TemporaryDirectory._rmtree for real impl
285+
# XXX: see Py's TemporaryDirectory._rmtree for real impl
287286

288287
proc close*(self: TemporaryDirectoryWrapper) =
289288
## used to be called in `with` stmt (Python's doesn't have this)

0 commit comments

Comments
 (0)