minor code cleanups (#21215)

This commit is contained in:
Andreas Rumpf
2023-01-02 10:39:17 +01:00
committed by GitHub
parent e9ed090c33
commit cf1b16ef8b
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.
## Each resource has an associated soft and hard limit, as defined by the RLimit structure
proc setrlimit*(resource: cint, rlp: var RLimit): cint
{.importc: "setrlimit",header: "<sys/resource.h>".}
proc setrlimit*(resource: cint, rlp: var RLimit): cint {.
importc: "setrlimit", header: "<sys/resource.h>".}
## The setrlimit() system calls sets resource limits.
proc getrlimit*(resource: cint, rlp: var RLimit): cint
{.importc: "getrlimit",header: "<sys/resource.h>".}
proc getrlimit*(resource: cint, rlp: var RLimit): cint {.
importc: "getrlimit", header: "<sys/resource.h>".}
## The getrlimit() system call gets resource limits.
when defined(nimHasStyleChecks):

View File

@@ -43,8 +43,8 @@ proc uname*(): Uname =
result.machine = charArrayToString u.machine
proc fsync*(fd: int) =
## synchronize a file's buffer cache to the storage device
if fsync(fd.cint) != 0:
## synchronize a file's buffer cache to the storage device
if fsync(fd.cint) != 0:
raise newException(OSError, $strerror(errno))
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.
var tmpl = cstring(prefix & "XXXXXX" & suffix)
let fd =
if len(suffix)==0:
if len(suffix) == 0:
when declared(mkostemp):
mkostemp(tmpl, O_CLOEXEC)
else:

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.
runnableExamples:
let a = [10, 20, 30]
var b: seq[(int, int)]
var b: seq[(int, int)] = @[]
for i, x in enumerate(a):
b.add((i, x))
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 =
const n = span(T)
var ret: array[n, uint8]
var i = 0
assert n <= invalidSlot.int
var ret {.noinit.}: array[n, uint8]
for ai in mitems(ret): ai = invalidSlot
for ai in items(T):
ret[ai.ord - T.low.ord] = uint8(i)

View File

@@ -94,8 +94,10 @@ when not defined(nimscript):
assert getEnv("unknownEnv", "doesn't exist") == "doesn't exist"
let env = getEnvImpl(key)
if env == nil: return default
result = $env
if env == nil:
result = default
else:
result = $env
proc existsEnv*(key: string): bool {.tags: [ReadEnvEffect].} =
## Checks whether the environment variable named `key` exists.
@@ -109,7 +111,7 @@ when not defined(nimscript):
runnableExamples:
assert not existsEnv("unknownEnv")
return getEnvImpl(key) != nil
result = getEnvImpl(key) != nil
proc putEnv*(key, val: string) {.tags: [WriteEnvEffect].} =
## Sets the value of the `environment variable`:idx: named `key` to `val`.
@@ -140,7 +142,7 @@ when not defined(nimscript):
## * `envPairs iterator`_
template bail = raiseOSError(osLastError(), key)
when defined(windows):
#[
#[
# 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
note that nil is not legal
@@ -177,20 +179,17 @@ when not defined(nimscript):
iterator envPairsImpl(): tuple[key, value: string] {.tags: [ReadEnvEffect].} =
when defined(windows):
block implBlock:
template impl(get_fun, typ, size, zero, free_fun) =
let env = get_fun()
var e = env
if e == nil: break implBlock
while true:
let eend = strEnd(e)
let kv = $e
let p = find(kv, '=')
yield (substr(kv, 0, p-1), substr(kv, p+1))
e = cast[typ](cast[int](eend)+size)
if typeof(zero)(eend[1]) == zero: break
discard free_fun(env)
impl(getEnvironmentStringsW, WideCString, 2, 0, freeEnvironmentStringsW)
let env = getEnvironmentStringsW()
var e = env
if e != nil:
while true:
let eend = strEnd(e)
let kv = $e
let p = find(kv, '=')
yield (substr(kv, 0, p-1), substr(kv, p+1))
e = cast[WideCString](cast[ByteAddress](eend)+2)
if int(eend[1]) == 0: break
discard freeEnvironmentStringsW(env)
else:
var i = 0
when defined(macosx) and not defined(ios) and not defined(emscripten):

View File

@@ -75,17 +75,14 @@ proc newOSError*(
## See also:
## * `osErrorMsg proc`_
## * `osLastError proc`_
var e: owned(ref OSError); new(e)
e.errorCode = errorCode.int32
e.msg = osErrorMsg(errorCode)
result = (ref OSError)(errorCode: errorCode.int32, msg: osErrorMsg(errorCode))
if additionalInfo.len > 0:
if e.msg.len > 0 and e.msg[^1] != '\n': e.msg.add '\n'
e.msg.add "Additional info: "
e.msg.add additionalInfo
if result.msg.len > 0 and result.msg[^1] != '\n': result.msg.add '\n'
result.msg.add "Additional info: "
result.msg.add additionalInfo
# don't add trailing `.` etc, which negatively impacts "jump to file" in IDEs.
if e.msg == "":
e.msg = "unknown OS error"
return e
if result.msg == "":
result.msg = "unknown OS error"
proc raiseOSError*(errorCode: OSErrorCode, additionalInfo = "") {.noinline.} =
## Raises an `OSError exception <system.html#OSError>`_.