simple unit test library

2019-11-30 Thread Matthew
Hi, I know this is trivial, but would you mind including a license like MIT 
with the code? (Also it'd be cool to have it uploaded to a tiny repo on GitHub 
in case anyone wants to suggest small improvements)

P.S. Sorry for reviving a 5 year old thread :P


Re: ``Table.take`` should be ``Table.pop`` -- discuss

2019-11-30 Thread Araq
But then we also need `mpop` "for consistency", it never ends...


Re: How to debug a segmentation fault?

2019-11-30 Thread doofenstein
do you iterate over the array? Then it might be related to 
[https://github.com/nim-lang/Nim/issues/12747](https://github.com/nim-lang/Nim/issues/12747)


Re: How to debug a segmentation fault?

2019-11-30 Thread spip
Thanks, it was effectively a stack overflow. Having a ref array instead of an 
array defined for the matrix type used as deeper procs' parameters did the 
trick.


Re: ``Table.take`` should be ``Table.pop`` -- discuss

2019-11-30 Thread b3liever
Well I think a pop proc would look like this instead:


proc pop*[A, B](t: var Table[A, B]): (A, B) =
  if len(t) == 0:
 raise newException(KeyError, "pop(): dictionary is empty")
  for h in countdown(high(t.data), 0):
 if isFilled(t.data[h].hcode):
   result (t.data[h].key, t.data[h].val)
   t.del(result[0])


Run

What do you think?


Re: recvmsg on AsyncSocket

2019-11-30 Thread cumulonimbus
> I'm curious what you're trying to achieve at a high level though, maybe there 
> is another way that doesn't require FDs being sent across processes?

@AMIGrAve already answered, but I'd like to add: This is the most efficient way 
to do hand-off between processes if you can do, whether it is a listening 
socket or a connected one.

If all is symmetric, (e.g. multiprocess nginx), then you can have multiple 
processes listening on the same port; Another common way to deal with this is 
the old "accept-and-fork", which works very well for long lived connections 
where overall processing dominates the forking time -- but not with e.g. 
preforking or long-lived backends.

Unfortunately, the most common way to deal with it -- especially if the backend 
is itself a long-lived process -- is for the original accepting process to 
proxy to the actual backend. This is wasteful in cpu , memory, kernel 
resources, and also requires the proxying process to remain up and responsive 
(adding latency and reducing reliability). While this is a necessary evil if 
backends are on a different machine, it is a complete waste if all is running 
on the same machine - and with 32-64 core servers relatively common these days 
(especially with containers), it is often the case that you can use the more 
effiicient route.

The other way to avoid sending FDs is shared memory - which is just a bit less 
efficient than transferring the FD (basically, just another memory copy and 
almost uncontended synchronization), but still has the latency/reliability 
drawback, and is significantly more complicated to get right.

It's available on Linux and most Unix variants, but not on Windows AFAIK.


Re: How to debug a segmentation fault?

2019-11-30 Thread Araq
> Is there something I can try before creating a minimal example and opening a 
> bug?

Reduce the involved data structure sizes and see if the crash disappears. If 
so, you might simply put too much data onto the stack causing a crash.


How to debug a segmentation fault?

2019-11-30 Thread spip
My program does not do tricky system things, only using ref object to build a 
toroid matrix, letting the compiler initialize all the objects. It segfaults 
when building the matrix, without any stack trace. I've tried placing echo and 
debugEcho in calls to get how far in the code it runs, but apart from the first 
message before the first call, I don't get any hint where the problem is: no 
message printed. I've tried changing the garbage collector too without success. 
gdb shows the proc call. This behaviours happens with Nim 0.19 and 1.0.4. With 
version 1.0.0, I had an assert message about a seq length having changed in an 
iterator, but this message disappears when compiled with version 1.0.4.

Safe programs in Nim aren't supposed to end cleanly or abort with a stack 
trace, or at least a debug message?


(gdb) run
Starting program: /home/pierre/sdk/sdk3
Let's go to solve...

Program received signal SIGSEGV, Segmentation fault.
0x55575264 in solve__1I5XRgZ0uR8B7KpdLW9aiTA (board=0x5578d420 
,
Result=0x5578d780 ) at 
/home/pierre/sdk/sdk3.nim:323
323 proc solve(board: Board): Board =
(gdb) bt
#0  0x55575264 in solve__1I5XRgZ0uR8B7KpdLW9aiTA 
(board=0x5578d420 ,
Result=0x5578d780 ) at 
/home/pierre/sdk/sdk3.nim:323
#1  0x555758d1 in NimMainModule () at /home/pierre/sdk/sdk3.nim:421
#2  0x55575953 in NimMainInner () at 
/home/pierre/.choosenim/toolchains/nim-1.0.4/lib/system.nim:3630
#3  0x55575e4f in NimMain () at 
/home/pierre/.choosenim/toolchains/nim-1.0.4/lib/system.nim:3638
#4  0x55575e86 in main (argc=, args=, 
env=)
at /home/pierre/.choosenim/toolchains/nim-1.0.4/lib/system.nim:3645
(gdb) list
318 let c = ans1 mod SIZE
319 let num = chr((ans2 mod SIZE) + 1)
320 result[r][c] = num
321
322
323 proc solve(board: Board): Board =
324   debugEcho "Before convert"
325   let cover = convertInCoverMatrix(board)
326   debugEcho "new DLX"
327   var dlx = newDLX(cover)


Run

Is there something I can try before creating a minimal example and opening a 
bug?


How to create a trojan and reverse shell with NIM

2019-11-30 Thread navicstein
Hello, am new to nim programming language, and i have a little knowledge of how 
to create a trojan virus with python, i have already implemented it in python 
but i feel like python is not _good enough_ i would like to know how replicate 
it in nim-lang, i already know that i will create a reverse shell somehow with 
sockets but the docs did not specify the client and server handshake and i 
could not find suitable materials online, am choosing nim because of the 
compile size and equivalent to c++ (which i don't know)


Re: onSignal closure memory violation

2019-11-30 Thread SleepyMan
Thanks @Araq, addQuitProc is one the procs I was looking for. I don't thinks 
I'll hit limit of 30 procs, so It'll probably suffice. Is there anything more 
generic that I can use for SIGHUP, to reload?

I'm implementing a queue of procs to call when SIGHUP is received, but adding 
an obj to a global seq from an inherited instance raises ObjectAssignmentError 
in lib/system/assign.nim(97) genericAssignAux. I can add a ptr obj, but when I 
cast it back, it "loses" the inheritance, and doesn't call the right method.

A minimal example:


type
  App* = object of RootObj
running*: bool
  
  PType = proc(app: App): bool {.nimcall.}
  PTuple = tuple[app: App, f: PType]
  
  BApp = object of App

var s = newSeq[PTuple]()

method reload*(app: App): bool {.base.} =
  echo "App reloaded"
  true

proc start*(app: App) =
  s.add((app, reload))
  echo repr(s)

proc stop*(app: App) =
  for e in s:
discard e.f(cast[App](e.app))

method reload*(app: var App): bool =
  echo "BApp reloaded"
  return true

when isMainModule:
  var app = BApp()
  app.start
  app.stop


Run


Re: Advent of Nim 2019 megathread

2019-11-30 Thread miran
12 hours to go!


Re: onSignal closure memory violation

2019-11-30 Thread Araq
For process cleanups use `addQuitProc`. 


Re: Returning a `var` inside of a tuple

2019-11-30 Thread Araq
The signature is weird anyway, how about:


proc insertsOrUpdates*[K, V](rbt: RedBlackTree[K, V], key: K, value: var 
V): bool


Run


Re: Returning a `var` inside of a tuple

2019-11-30 Thread iocat
Thanks for replying.

Sorry but that's kinda not what I intended the function to return :(. Because I 
don't have a lvalue for "inserted" part of the tuple so that it can fit in into 
a var. Right (?) 


Re: Returning a `var` inside of a tuple

2019-11-30 Thread mratsim
I think the whole tuple needs to be var.