Skip to content

Commit 538277f

Browse files
committed
fix(oserr): windows: raiseExcWithPath used wrong error code
... didn't cast winerror code to errno before exc map
1 parent 364bea5 commit 538277f

File tree

3 files changed

+13
-9
lines changed

3 files changed

+13
-9
lines changed

src/pylib/pyerrors/oserr.nim

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,10 @@ template noWeirdTarget*(def) =
7070

7171
proc raiseExcWithPath*(p: PathLike, errCode: OSErrorCode){.sideEffect.} =
7272
## raises OSError or its one of SubError type
73-
raise OSError_new[oserrors_types.PyOSError](true, errCode.cint, p.fspath)
73+
raise when defined(windows):
74+
OSError_new[oserrors_types.PyOSError](0, p.fspath, winerror=errCode.cint)
75+
else:
76+
OSError_new[oserrors_types.PyOSError](errCode.cint, p.fspath)
7477
7578
proc raiseExcWithPath*(p: PathLike){.sideEffect.} =
7679
let oserr = osLastError()
@@ -119,7 +122,7 @@ elif not weirdTarget:
119122
func errnoMsg*(errno: cint): string = $c_strerror(errno)
120123
121124
proc newErrnoErrT[E: PyOSError](errno=getErrno(), strerr: string): owned(ref PyOSError) =
122-
OSError_new[E](false, errno, strerr)
125+
OSError_new[E](errno, strerr)
123126
proc newErrnoErrT[E: PyOSError](errno=getErrno()): owned(ref PyOSError) =
124127
newErrnoErrT[E](errno, errnoMsg(errno))
125128
@@ -136,11 +139,11 @@ proc raiseErrnoT*[T: PyOSError](errno=getErrno()) =
136139
proc raiseErrnoWithPath*[T](p: PathLike[T]; errno = getErrno()) =
137140
## raises OSError or its SubError.
138141
## refer to errno even under Windows.
139-
raise OSError_new[oserrors_types.PyOSError](false, errno, errnoMsg(errno), p.fspath)
142+
raise OSError_new[oserrors_types.PyOSError](errno, errnoMsg(errno), p.fspath)
140143
141144
when InJs:
142145
proc raiseErrnoWithMsg*(errno: cint, errMsg: string) =
143-
raise OSError_new[oserrors_types.PyOSError](false, errno, errMsg, fillMsg=false)
146+
raise OSError_new[oserrors_types.PyOSError](errno, errMsg, fillMsg=false)
144147
145148
template catchJsErrAndDo(doSth; doErr) =
146149
var errMsg = ""

src/pylib/pyerrors/oserr/init.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import ./oserror_new
55
template gen(name, typ, thirdDefVal){.dirty.} =
66
proc name*(myerrno: cint, strerr: string,
77
filename = thirdDefVal, winerror: cint = 0, filename2 = ""): ref PyOSError =
8-
OSError_new[types.typ](true, myerrno, strerr, filename, winerror, filename2)
8+
OSError_new[types.typ](myerrno, strerr, filename, winerror, filename2)
99
proc name*(strerr: string = ""): ref PyOSError =
1010
new result
1111
result.strerror = strerr

src/pylib/pyerrors/oserr/oserror_new.nim

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@ type
2828
template PyNumber_Check(x): bool = x is int
2929
template PyNumber_AsSsize_t(x, _): int = x
3030

31-
template parseOSErrorArgs(args: var OSErrorArgs) =
31+
template parseOSErrorArgs(args: var OSErrorArgs, useWinError) =
3232
## Parses arguments for OSError construction
3333
## Returns tuple containing errno, strerror, filename, filename2, and winerror (on Windows)
3434
## Note: This function doesn't cleanup on error, the caller should
3535
when defined(windows):
3636
args.winerror = winerror
37-
if args.winerror != 0:
37+
if useWinError:
3838
args.errno = winerror_to_errno(args.winerror)
3939

4040
template oserror_use_init*[E: PyOSError](self): bool =
@@ -64,16 +64,17 @@ proc init*[E: PyOSError](self: ref E, args: OSErrorArgs) =
6464
when defined(windows):
6565
self.winerror = args.winerror
6666
67-
proc OSError_new*[E: PyOSError](useWinError: bool, myerrno: cint, strerr: string,
67+
proc OSError_new*[E: PyOSError](myerrno: cint, strerr: string,
6868
filename: string|int = "", winerror: cint = 0, filename2 = "", fillMsg: static[bool] = true): ref PyOSError =
6969
## may returns a subclass of OSError
7070
type Third = typeof(filename)
7171
var args: OSErrorArgs[Third] = (myerrno, strerr, filename, winerror, filename2)
7272
var newtype = proc (): ref PyOSError = new E
7373
let use_init = oserror_use_init[E](result)
7474
if not use_init:
75+
let useWinError = winerror != 0
7576
if useWinError:
76-
parseOSErrorArgs(args)
77+
parseOSErrorArgs(args, useWinError)
7778
when E is PyOSError:
7879
newtype = errnomap.getOrDefault(args.errno, default_oserror)
7980
result = newtype()

0 commit comments

Comments
 (0)