minor code cleanups (#21215)

This commit is contained in:
Andreas Rumpf 2023-01-02 10:39:17 +01:00 • committed by GitHub
commit cf1b16ef8b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 32 additions and 36 deletions

View file

@ -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):

View file

@ -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)]

View file

@ -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)

View file

@ -94,7 +94,9 @@ 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 = default
else:
result = $env result = $env
proc existsEnv*(key: string): bool {.tags: [ReadEnvEffect].} = proc existsEnv*(key: string): bool {.tags: [ReadEnvEffect].} =
@ -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`.
@ -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) =
let env = get_fun()
var e = env var e = env
if e == nil: break implBlock if e != nil:
while true: while true:
let eend = strEnd(e) let eend = strEnd(e)
let kv = $e let kv = $e
let p = find(kv, '=') let p = find(kv, '=')
yield (substr(kv, 0, p-1), substr(kv, p+1)) yield (substr(kv, 0, p-1), substr(kv, p+1))
e = cast[typ](cast[int](eend)+size) e = cast[WideCString](cast[ByteAddress](eend)+2)
if typeof(zero)(eend[1]) == zero: break if int(eend[1]) == 0: break
discard free_fun(env) discard freeEnvironmentStringsW(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):

View file

@ -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>`_.