@@ -15,7 +15,7 @@ when defined(js):
15
15
template decl_c_intImpl(variable, name, _; defval) =
16
16
let variable = from_js_const(name, defval)
17
17
{.pragma: ErrnoMapAttr.}
18
- template ifHasErr(name, body) =
18
+ template ifHasErr(name: int ; body) =
19
19
if name != DEF_INT: body
20
20
else :
21
21
import ../../ pyconfig/ util
24
24
template decl_c_intImpl(variable, name, includeFile; defval) =
25
25
const variable = from_c_int(name, includeFile, defval)
26
26
{.pragma: ErrnoMapAttr, compileTime.}
27
- template ifHasErr(name, body) =
27
+ template ifHasErr(name: int ; body) =
28
28
when name != DEF_INT: body
29
29
30
30
template decl_c_int* (name, includeFile; defval) =
@@ -38,20 +38,32 @@ var errnomap*{.ErrnoMapAttr.}: Table[cint, proc(): ref PyOSError{.OSErrorInitAtt
38
38
proc default_oserror* (): ref PyOSError{.OSErrorInitAttrs.} =
39
39
new PyOSError
40
40
41
- template decl_err(err){.dirty.} =
42
- decl_c_int( err, " <errno.h>" , DEF_INT)
41
+ template decl_err(variable, err){.dirty.} =
42
+ decl_c_intImpl(variable, err, " <errno.h>" , DEF_INT)
43
43
44
- template decl_err_cint(err){.dirty.} =
45
- when declared(err):
44
+ template isConst(e): bool = compiles((const _= err))
45
+
46
+ template asgExp(err; nerr: int ; doSth) =
47
+ asgn_cint err, cast [cint ](nerr)
48
+ export err
49
+ doSth
50
+
51
+ template decl_err_cint(err, doIfDefined){.dirty.} =
52
+ # # generate `const err*: cint = ...` if err defined in `<errno.h>`
53
+ bind asgExp
54
+ when isConst(err):
46
55
export err
56
+ doIfDefined
47
57
else :
48
- decl_c_intImpl(`n err`, name, " <errno.h>" , DEF_INT)
49
- template asgExp =
50
- asgn_cint err, cast [cint ](`n err`)
51
- export err
52
- when defined(js): asgExp
58
+ decl_err(`n err`, err)
59
+ when defined(js): asgExp err, `n err`, doIfDefined
53
60
else :
54
- ifHasErr `n err`: asgExp
61
+ ifHasErr `n err`: asgExp err, `n err`, doIfDefined
62
+
63
+ template decl_err_cint(err) =
64
+ # # generate `const err*: cint = ...` if err defined in `<errno.h>`
65
+ bind decl_err_cint
66
+ decl_err_cint(err): discard
55
67
56
68
decl_err_cint E2BIG
57
69
decl_err_cint ENOEXEC
@@ -66,12 +78,8 @@ decl_err_cint EINVAL
66
78
67
79
when true : # PyExc_InitState
68
80
template ADD_ERRNO(exc, err){.dirty.} =
69
- when not declared(err):
70
- decl_err(err)
71
- else :
72
- export err
73
- ifHasErr err:
74
- errnomap[cast [cint ](err)] = proc (): ref PyOSError = new exc
81
+ decl_err_cint err:
82
+ errnomap[err] = proc (): ref PyOSError = new exc
75
83
# The following is just copied from CPython's PyExc_InitState AS-IS.
76
84
77
85
ADD_ERRNO(BlockingIOError, EAGAIN);
0 commit comments