Skip to content

Commit

Permalink
Merge pull request nim-lang#1 from singularperturbation/fix-2573
Browse files Browse the repository at this point in the history
osLastError() and whitespace
  • Loading branch information
HaCk3Dq committed Jul 3, 2015
2 parents 5c7c5f8 + bf08ed8 commit a73778a
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 53 deletions.
9 changes: 5 additions & 4 deletions lib/pure/net.nim
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ proc getSocketError*(socket: Socket): OSErrorCode =
if result == 0.OSErrorCode:
result = socket.lastError
if result == 0.OSErrorCode:
raiseOSError(OSError)
raiseOSError(osLastError())

proc socketError*(socket: Socket, err: int = -1, async = false,
lastError = (-1).OSErrorCode) =
Expand Down Expand Up @@ -295,10 +295,11 @@ proc socketError*(socket: Socket, err: int = -1, async = false,
else:
let errStr = $ErrErrorString(sslErr, nil)
raiseSSLError(errStr & ": " & errStr)
let osMsg = osErrorMsg osLastError()
let osErr = osLastError
let osMsg = osErrorMsg osErr
if osMsg != "":
errStr.add ". The OS reports: " & osMsg
raiseOSError(OSError)
raiseOSError(osErr)
of SSL_ERROR_SSL:
raiseSSLError()
else: raiseSSLError("Unknown Error")
Expand Down Expand Up @@ -884,7 +885,7 @@ proc send*(socket: Socket, data: string,
socketError(socket, lastError = lastError)

if sent != data.len:
raiseOSError(OSError)
raiseOSError(osLastError())

proc trySend*(socket: Socket, data: string): bool {.tags: [WriteIOEffect].} =
## Safe alternative to ``send``. Does not raise an EOS when an error occurs,
Expand Down
4 changes: 2 additions & 2 deletions lib/pure/os.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1019,7 +1019,7 @@ proc moveFile*(source, dest: string) {.rtl, extern: "nos$1",
if moveFileA(source, dest, 0'i32) == 0'i32: raiseOSError(osLastError())
else:
if c_rename(source, dest) != 0'i32:
raiseOSError(OSError)
raiseOSError(osLastError())

when not declared(ENOENT) and not defined(Windows):
when NoFakeVars:
Expand Down Expand Up @@ -1054,7 +1054,7 @@ proc removeFile*(file: string) {.rtl, extern: "nos$1", tags: [WriteDirEffect].}
raiseOSError(osLastError())
else:
if c_remove(file) != 0'i32 and errno != ENOENT:
raiseOSError(OSError)
raiseOSError(osLastError())

proc execShellCmd*(command: string): int {.rtl, extern: "nos$1",
tags: [ExecIOEffect].} =
Expand Down
38 changes: 19 additions & 19 deletions lib/pure/rawsockets.nim
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ when useWinVersion:
FIONBIO* = IOC_IN.int32 or ((sizeof(int32) and IOCPARM_MASK) shl 16) or
(102 shl 8) or 126

proc ioctlsocket*(s: SocketHandle, cmd: clong,
proc ioctlsocket*(s: SocketHandle, cmd: clong,
argptr: ptr clong): cint {.
stdcall, importc: "ioctlsocket", dynlib: "ws2_32.dll".}
else:
Expand Down Expand Up @@ -141,7 +141,7 @@ when not useWinVersion:
else: discard

else:
proc toInt(domain: Domain): cint =
proc toInt(domain: Domain): cint =
result = toU16(ord(domain))

proc toInt(typ: SockType): cint =
Expand Down Expand Up @@ -199,12 +199,12 @@ proc getAddrInfo*(address: string, port: Port, af: Domain = AF_INET, typ: SockTy
when useWinVersion:
raiseOSError(osLastError())
else:
raiseOSError(OSError)
raiseOSError(osLastError())

proc dealloc*(ai: ptr AddrInfo) =
freeaddrinfo(ai)

proc ntohl*(x: int32): int32 =
proc ntohl*(x: int32): int32 =
## Converts 32-bit integers from network to host byte order.
## On machines where the host byte order is the same as network byte order,
## this is a no-op; otherwise, it performs a 4-byte swap operation.
Expand Down Expand Up @@ -243,13 +243,13 @@ proc getServByName*(name, proto: string): Servent {.tags: [ReadIOEffect].} =
var s = winlean.getservbyname(name, proto)
else:
var s = posix.getservbyname(name, proto)
if s == nil: raiseOSError(OSError)
if s == nil: raiseOSError(osLastError())
result.name = $s.s_name
result.aliases = cstringArrayToSeq(s.s_aliases)
result.port = Port(s.s_port)
result.proto = $s.s_proto

proc getServByPort*(port: Port, proto: string): Servent {.tags: [ReadIOEffect].} =
proc getServByPort*(port: Port, proto: string): Servent {.tags: [ReadIOEffect].} =
## Searches the database from the beginning and finds the first entry for
## which the port specified by ``port`` matches the s_port member and the
## protocol name specified by ``proto`` matches the s_proto member.
Expand All @@ -259,7 +259,7 @@ proc getServByPort*(port: Port, proto: string): Servent {.tags: [ReadIOEffect].}
var s = winlean.getservbyport(ze(int16(port)).cint, proto)
else:
var s = posix.getservbyport(ze(int16(port)).cint, proto)
if s == nil: raiseOSError(OSError)
if s == nil: raiseOSError(osLastError())
result.name = $s.s_name
result.aliases = cstringArrayToSeq(s.s_aliases)
result.port = Port(s.s_port)
Expand All @@ -275,10 +275,10 @@ proc getHostByAddr*(ip: string): Hostent {.tags: [ReadIOEffect].} =
cint(rawsockets.AF_INET))
if s == nil: raiseOSError(osLastError())
else:
var s = posix.gethostbyaddr(addr(myaddr), sizeof(myaddr).Socklen,
var s = posix.gethostbyaddr(addr(myaddr), sizeof(myaddr).Socklen,
cint(posix.AF_INET))
if s == nil:
raiseOSError(OSError)
raiseOSError(osLastError())

result.name = $s.h_name
result.aliases = cstringArrayToSeq(s.h_aliases)
Expand All @@ -290,11 +290,11 @@ proc getHostByAddr*(ip: string): Hostent {.tags: [ReadIOEffect].} =
elif s.h_addrtype == posix.AF_INET6:
result.addrtype = AF_INET6
else:
raiseOSError(OSError)
raiseOSError(osLastError())
result.addrList = cstringArrayToSeq(s.h_addr_list)
result.length = int(s.h_length)

proc getHostByName*(name: string): Hostent {.tags: [ReadIOEffect].} =
proc getHostByName*(name: string): Hostent {.tags: [ReadIOEffect].} =
## This function will lookup the IP address of a hostname.
when useWinVersion:
var s = winlean.gethostbyname(name)
Expand All @@ -311,11 +311,11 @@ proc getHostByName*(name: string): Hostent {.tags: [ReadIOEffect].} =
elif s.h_addrtype == posix.AF_INET6:
result.addrtype = AF_INET6
else:
raiseOSError(OSError)
raiseOSError(osLastError())
result.addrList = cstringArrayToSeq(s.h_addr_list)
result.length = int(s.h_length)

proc getSockName*(socket: SocketHandle): Port =
proc getSockName*(socket: SocketHandle): Port =
## returns the socket's associated port number.
var name: Sockaddr_in
when useWinVersion:
Expand All @@ -331,11 +331,11 @@ proc getSockName*(socket: SocketHandle): Port =
result = Port(rawsockets.ntohs(name.sin_port))

proc getSockOptInt*(socket: SocketHandle, level, optname: int): int {.
tags: [ReadIOEffect].} =
tags: [ReadIOEffect].} =
## getsockopt for integer options.
var res: cint
var size = sizeof(res).SockLen
if getsockopt(socket, cint(level), cint(optname),
if getsockopt(socket, cint(level), cint(optname),
addr(res), addr(size)) < 0'i32:
raiseOSError(osLastError())
result = int(res)
Expand All @@ -344,7 +344,7 @@ proc setSockOptInt*(socket: SocketHandle, level, optname, optval: int) {.
tags: [WriteIOEffect].} =
## setsockopt for integer options.
var value = cint(optval)
if setsockopt(socket, cint(level), cint(optname), addr(value),
if setsockopt(socket, cint(level), cint(optname), addr(value),
sizeof(value).SockLen) < 0'i32:
raiseOSError(osLastError())

Expand All @@ -371,13 +371,13 @@ proc timeValFromMilliseconds(timeout = 500): Timeval =
result.tv_sec = seconds.int32
result.tv_usec = ((timeout - seconds * 1000) * 1000).int32

proc createFdSet(fd: var TFdSet, s: seq[SocketHandle], m: var int) =
proc createFdSet(fd: var TFdSet, s: seq[SocketHandle], m: var int) =
FD_ZERO(fd)
for i in items(s):
for i in items(s):
m = max(m, int(i))
FD_SET(i, fd)

proc pruneSocketSet(s: var seq[SocketHandle], fd: var TFdSet) =
proc pruneSocketSet(s: var seq[SocketHandle], fd: var TFdSet) =
var i = 0
var L = s.len
while i < L:
Expand Down
Loading

0 comments on commit a73778a

Please sign in to comment.