yglukhov / nimpy

Nim - Python bridge
MIT License
1.48k stars 60 forks source link

Hello world Segmentation fault #271

Closed HugoGranstrom closed 2 years ago

HugoGranstrom commented 2 years ago

I tried running the hello world example:

import nimpy/py_lib
pyInitLibPath("/home/hugo/miniconda3/lib/libpython3.9.so.1.0")

import nimpy

proc greet(name: string): string {.exportpy.} =
  return "Hello, " & name & "!"

I compiled it like this: nim c --app:lib --out:mymodule.so --threads:on mymodule.nim

And the Python code:

import mymodule
print(mymodule.greet("Hugo"))

But I'm getting this error when running the python file:

Traceback (most recent call last)
/home/hugo/.nimble/pkgs/nimpy-0.2.0/nimpy.nim(674) greetPy_wrapper
SIGSEGV: Illegal storage access. (Attempt to read from nil?)
Segmentation fault (core dumped)

It somehow seems to be the string (seq also fails, so feels like something with the GC) that's causing this, because if I change the function to this version with an int instead, it works:

proc greet(name: int): string {.exportpy.} =
  return "Hello, " & $name & "!"

I have tried compiling with arc as well, but the same error. Any idea why this is happening?

Versions

yglukhov commented 2 years ago

Please make absolutely sure you're using latest nimpy (delete and reinstall). Otherwise I cannot reproduce this.

HugoGranstrom commented 2 years ago

It seems like it was the pyInitLibPath("/home/hugo/miniconda3/lib/libpython3.9.so.1.0") that was the trouble. I fixed the thing that required me to have it, and now it works :) So it was just some local problems I guess.