minor code cleanups (#21215)
This commit is contained in:
parent
e9ed090c33
commit
cf1b16ef8b
6 changed files with 32 additions and 36 deletions
|
|
@ -1140,12 +1140,12 @@ type
|
||||||
## The getrlimit() and setrlimit() system calls get and set resource limits respectively.
|
## The getrlimit() and setrlimit() system calls get and set resource limits respectively.
|
||||||
## Each resource has an associated soft and hard limit, as defined by the RLimit structure
|
## Each resource has an associated soft and hard limit, as defined by the RLimit structure
|
||||||
|
|
||||||
proc setrlimit*(resource: cint, rlp: var RLimit): cint
|
proc setrlimit*(resource: cint, rlp: var RLimit): cint {.
|
||||||
{.importc: "setrlimit",header: "<sys/resource.h>".}
|
importc: "setrlimit", header: "<sys/resource.h>".}
|
||||||
## The setrlimit() system calls sets resource limits.
|
## The setrlimit() system calls sets resource limits.
|
||||||
|
|
||||||
proc getrlimit*(resource: cint, rlp: var RLimit): cint
|
proc getrlimit*(resource: cint, rlp: var RLimit): cint {.
|
||||||
{.importc: "getrlimit",header: "<sys/resource.h>".}
|
importc: "getrlimit", header: "<sys/resource.h>".}
|
||||||
## The getrlimit() system call gets resource limits.
|
## The getrlimit() system call gets resource limits.
|
||||||
|
|
||||||
when defined(nimHasStyleChecks):
|
when defined(nimHasStyleChecks):
|
||||||
|
|
|
||||||
|
|
@ -43,8 +43,8 @@ proc uname*(): Uname =
|
||||||
result.machine = charArrayToString u.machine
|
result.machine = charArrayToString u.machine
|
||||||
|
|
||||||
proc fsync*(fd: int) =
|
proc fsync*(fd: int) =
|
||||||
## synchronize a file's buffer cache to the storage device
|
## synchronize a file's buffer cache to the storage device
|
||||||
if fsync(fd.cint) != 0:
|
if fsync(fd.cint) != 0:
|
||||||
raise newException(OSError, $strerror(errno))
|
raise newException(OSError, $strerror(errno))
|
||||||
|
|
||||||
proc stat*(path: string): Stat =
|
proc stat*(path: string): Stat =
|
||||||
|
|
@ -90,7 +90,7 @@ proc mkstemp*(prefix: string, suffix=""): (string, File) =
|
||||||
## Returns the filename and a file opened in r/w mode.
|
## Returns the filename and a file opened in r/w mode.
|
||||||
var tmpl = cstring(prefix & "XXXXXX" & suffix)
|
var tmpl = cstring(prefix & "XXXXXX" & suffix)
|
||||||
let fd =
|
let fd =
|
||||||
if len(suffix)==0:
|
if len(suffix) == 0:
|
||||||
when declared(mkostemp):
|
when declared(mkostemp):
|
||||||
mkostemp(tmpl, O_CLOEXEC)
|
mkostemp(tmpl, O_CLOEXEC)
|
||||||
else:
|
else:
|
||||||
|
|
|
||||||
|
|
@ -21,7 +21,7 @@ macro enumerate*(x: ForLoopStmt): untyped {.since: (1, 3).} =
|
||||||
## The default starting count `0` can be manually overridden if needed.
|
## The default starting count `0` can be manually overridden if needed.
|
||||||
runnableExamples:
|
runnableExamples:
|
||||||
let a = [10, 20, 30]
|
let a = [10, 20, 30]
|
||||||
var b: seq[(int, int)]
|
var b: seq[(int, int)] = @[]
|
||||||
for i, x in enumerate(a):
|
for i, x in enumerate(a):
|
||||||
b.add((i, x))
|
b.add((i, x))
|
||||||
assert b == @[(0, 10), (1, 20), (2, 30)]
|
assert b == @[(0, 10), (1, 20), (2, 30)]
|
||||||
|
|
|
||||||
|
|
@ -112,9 +112,9 @@ const invalidSlot = uint8.high
|
||||||
|
|
||||||
proc genLookup[T: typedesc[HoleyEnum]](_: T): auto =
|
proc genLookup[T: typedesc[HoleyEnum]](_: T): auto =
|
||||||
const n = span(T)
|
const n = span(T)
|
||||||
var ret: array[n, uint8]
|
|
||||||
var i = 0
|
var i = 0
|
||||||
assert n <= invalidSlot.int
|
assert n <= invalidSlot.int
|
||||||
|
var ret {.noinit.}: array[n, uint8]
|
||||||
for ai in mitems(ret): ai = invalidSlot
|
for ai in mitems(ret): ai = invalidSlot
|
||||||
for ai in items(T):
|
for ai in items(T):
|
||||||
ret[ai.ord - T.low.ord] = uint8(i)
|
ret[ai.ord - T.low.ord] = uint8(i)
|
||||||
|
|
|
||||||
|
|
@ -94,8 +94,10 @@ when not defined(nimscript):
|
||||||
assert getEnv("unknownEnv", "doesn't exist") == "doesn't exist"
|
assert getEnv("unknownEnv", "doesn't exist") == "doesn't exist"
|
||||||
|
|
||||||
let env = getEnvImpl(key)
|
let env = getEnvImpl(key)
|
||||||
if env == nil: return default
|
if env == nil:
|
||||||
result = $env
|
result = default
|
||||||
|
else:
|
||||||
|
result = $env
|
||||||
|
|
||||||
proc existsEnv*(key: string): bool {.tags: [ReadEnvEffect].} =
|
proc existsEnv*(key: string): bool {.tags: [ReadEnvEffect].} =
|
||||||
## Checks whether the environment variable named `key` exists.
|
## Checks whether the environment variable named `key` exists.
|
||||||
|
|
@ -109,7 +111,7 @@ when not defined(nimscript):
|
||||||
runnableExamples:
|
runnableExamples:
|
||||||
assert not existsEnv("unknownEnv")
|
assert not existsEnv("unknownEnv")
|
||||||
|
|
||||||
return getEnvImpl(key) != nil
|
result = getEnvImpl(key) != nil
|
||||||
|
|
||||||
proc putEnv*(key, val: string) {.tags: [WriteEnvEffect].} =
|
proc putEnv*(key, val: string) {.tags: [WriteEnvEffect].} =
|
||||||
## Sets the value of the `environment variable`:idx: named `key` to `val`.
|
## Sets the value of the `environment variable`:idx: named `key` to `val`.
|
||||||
|
|
@ -140,7 +142,7 @@ when not defined(nimscript):
|
||||||
## * `envPairs iterator`_
|
## * `envPairs iterator`_
|
||||||
template bail = raiseOSError(osLastError(), key)
|
template bail = raiseOSError(osLastError(), key)
|
||||||
when defined(windows):
|
when defined(windows):
|
||||||
#[
|
#[
|
||||||
# https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/putenv-s-wputenv-s?view=msvc-160
|
# https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/putenv-s-wputenv-s?view=msvc-160
|
||||||
> You can remove a variable from the environment by specifying an empty string (that is, "") for value_string
|
> You can remove a variable from the environment by specifying an empty string (that is, "") for value_string
|
||||||
note that nil is not legal
|
note that nil is not legal
|
||||||
|
|
@ -177,20 +179,17 @@ when not defined(nimscript):
|
||||||
|
|
||||||
iterator envPairsImpl(): tuple[key, value: string] {.tags: [ReadEnvEffect].} =
|
iterator envPairsImpl(): tuple[key, value: string] {.tags: [ReadEnvEffect].} =
|
||||||
when defined(windows):
|
when defined(windows):
|
||||||
block implBlock:
|
let env = getEnvironmentStringsW()
|
||||||
template impl(get_fun, typ, size, zero, free_fun) =
|
var e = env
|
||||||
let env = get_fun()
|
if e != nil:
|
||||||
var e = env
|
while true:
|
||||||
if e == nil: break implBlock
|
let eend = strEnd(e)
|
||||||
while true:
|
let kv = $e
|
||||||
let eend = strEnd(e)
|
let p = find(kv, '=')
|
||||||
let kv = $e
|
yield (substr(kv, 0, p-1), substr(kv, p+1))
|
||||||
let p = find(kv, '=')
|
e = cast[WideCString](cast[ByteAddress](eend)+2)
|
||||||
yield (substr(kv, 0, p-1), substr(kv, p+1))
|
if int(eend[1]) == 0: break
|
||||||
e = cast[typ](cast[int](eend)+size)
|
discard freeEnvironmentStringsW(env)
|
||||||
if typeof(zero)(eend[1]) == zero: break
|
|
||||||
discard free_fun(env)
|
|
||||||
impl(getEnvironmentStringsW, WideCString, 2, 0, freeEnvironmentStringsW)
|
|
||||||
else:
|
else:
|
||||||
var i = 0
|
var i = 0
|
||||||
when defined(macosx) and not defined(ios) and not defined(emscripten):
|
when defined(macosx) and not defined(ios) and not defined(emscripten):
|
||||||
|
|
|
||||||
|
|
@ -75,17 +75,14 @@ proc newOSError*(
|
||||||
## See also:
|
## See also:
|
||||||
## * `osErrorMsg proc`_
|
## * `osErrorMsg proc`_
|
||||||
## * `osLastError proc`_
|
## * `osLastError proc`_
|
||||||
var e: owned(ref OSError); new(e)
|
result = (ref OSError)(errorCode: errorCode.int32, msg: osErrorMsg(errorCode))
|
||||||
e.errorCode = errorCode.int32
|
|
||||||
e.msg = osErrorMsg(errorCode)
|
|
||||||
if additionalInfo.len > 0:
|
if additionalInfo.len > 0:
|
||||||
if e.msg.len > 0 and e.msg[^1] != '\n': e.msg.add '\n'
|
if result.msg.len > 0 and result.msg[^1] != '\n': result.msg.add '\n'
|
||||||
e.msg.add "Additional info: "
|
result.msg.add "Additional info: "
|
||||||
e.msg.add additionalInfo
|
result.msg.add additionalInfo
|
||||||
# don't add trailing `.` etc, which negatively impacts "jump to file" in IDEs.
|
# don't add trailing `.` etc, which negatively impacts "jump to file" in IDEs.
|
||||||
if e.msg == "":
|
if result.msg == "":
|
||||||
e.msg = "unknown OS error"
|
result.msg = "unknown OS error"
|
||||||
return e
|
|
||||||
|
|
||||||
proc raiseOSError*(errorCode: OSErrorCode, additionalInfo = "") {.noinline.} =
|
proc raiseOSError*(errorCode: OSErrorCode, additionalInfo = "") {.noinline.} =
|
||||||
## Raises an `OSError exception <system.html#OSError>`_.
|
## Raises an `OSError exception <system.html#OSError>`_.
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue