Skip to content

Commit bad24cc

Browse files
committed
fix(Lib/resource): getpagesize not compile; prlimit,setrlimit not work for non tuple types
1 parent 5f26625 commit bad24cc

File tree

1 file changed

+32
-13
lines changed

1 file changed

+32
-13
lines changed

src/pylib/Lib/resource_impl/funcs.nim

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,13 @@ proc py2rlimit(limits: py_rlimit, rl_out: var RLimit) =
2727
rl_out.rlim_cur = limits.rlim_cur
2828
rl_out.rlim_max = limits.rlim_max
2929

30-
proc py2rlimit(limits: py_rlimit_abc, rl_out: var RLimit) =
30+
template py2rlimit[T](limits: T{atom}, rl_out: var RLimit) =
3131
assert limits.len == 2
3232
rl_out.rlim_cur = limits[0]
3333
rl_out.rlim_max = limits[1]
34+
template py2rlimit[T](limits: T, rl_out: var RLimit) =
35+
let li = limits
36+
py2rlimit(li, rl_out)
3437
3538
proc rlimit2py(rl_in: RLimit): py_rlimit = (rl_in.rlim_cur, rl_in.rlim_max)
3639
@@ -49,15 +52,23 @@ proc getrlimit*(resource: int): py_rlimit =
4952
5053
proc raise_inval =
5154
raise newException(ValueError, "current limit exceeds maximum limit")
52-
proc setrlimit*(resource: int, limits: py_rlimit_abc) =
53-
var rl: RLimit
54-
py2rlimit(limits, rl)
55+
56+
proc setrlimitWrap(resource: int, rl: var RLimit) =
5557
if setrlimit(checked_resource(resource), rl) == -1:
5658
if isErr(EINVAL): raise_inval()
5759
elif isErr(EPERM):
5860
raise newException(ValueError, "not allowed to raise maximum limit")
5961
raiseErrno()
6062
63+
template setrlimit*[T: py_rlimit_abc|py_rlimit](resource: int, limits: T) =
64+
## this is defined as `template`.
65+
## Because if being `proc`, py_rlimit_abc match cannot work
66+
bind py2rlimit, setrlimitWrap
67+
mixin len, `[]`
68+
var rl: RLimit
69+
py2rlimit(limits, rl)
70+
setrlimitWrap(resource, rl)
71+
6172
when HAVE_PRLIMIT:
6273
proc prlimit(pid: Pid, resource: cint, new_limit: ptr RLimit, old_limit: var RLimit): cint {.
6374
importc, header: "<sys/resource.h>".}
@@ -72,21 +83,29 @@ when HAVE_PRLIMIT:
7283
raiseErrno()
7384
rlimit2py(old_limit)
7485
75-
proc prlimit*(pid: int, resource: int, limits: py_rlimit_abc): py_rlimit{.discardable.} =
76-
let
77-
pid = Pid pid
78-
resource = checked_resource(resource)
79-
var old_limit, new_limit: RLimit
80-
81-
py2rlimit(limits, new_limit)
86+
proc prlimitWrap(pid: Pid, resource: cint, new_limit: var RLimit): py_rlimit{.discardable.} =
87+
var old_limit: RLimit
8288
if prlimit(pid, resource, addr new_limit, old_limit) == -1:
8389
if isErr(EINVAL): raise_inval()
8490
raiseErrno()
85-
8691
rlimit2py(old_limit)
8792
93+
template prlimit*[T: py_rlimit_abc|py_rlimit](pid: int, resource: int, limits: T): py_rlimit =
94+
## discardable.
95+
##
96+
## this is defined as `template`.
97+
## Because if being `proc`, py_rlimit_abc match cannot work
98+
bind Pid, checked_resource, RLimit, py2rlimit, prlimitWrap
99+
mixin len, `[]`
100+
let
101+
tpid = Pid pid
102+
tresource = checked_resource(resource)
103+
var new_limit: RLimit
104+
py2rlimit(limits, new_limit)
105+
prlimitWrap(tpid, tresource, new_limit)
106+
88107
when HAVE_GETPAGESIZE:
89-
proc c_getpagesize(): cint{.importc, header: "<unistd.h>".}
108+
proc c_getpagesize(): cint{.importc: "getpagesize", header: "<unistd.h>".}
90109
proc getpagesize*(): int = int c_getpagesize()
91110
elif HAVE_SYSCONF_PAGE_SIZE:
92111
let SC_PAGE_SIZE{.importc, header: "<unistd.h>".}: cint

0 commit comments

Comments
 (0)