nimpy icon indicating copy to clipboard operation
nimpy copied to clipboard

`SIGSEGV: Illegal storage access` error when trying to `echo py.exec("invalid python")`

Open M-MAD-Official opened this issue 4 years ago • 1 comments

import
  rdstdin,
  nimpy

let py = pyBuiltinsModule()

proc repl*(prompt: string = "$ ", welcome_message: string = "") =
  if welcome_message != "": echo welcome_message
  while true:
    try:
      let input: auto = readLineFromStdin(prompt)
      var output: PyObject
      try:
        output = py.exec(input)
      except Exception:
        echo "Exception! ", repr(getCurrentException()[])
      echo type(output)
      echo output
    except IOError:
      quit 0

This code compiles; but when I type invalid python in repl, this happens:

...
/home/user/Documents/Nim/repl.nim(82) repl (It's the `echo $output` line)
/home/user/.nimble/pkgs/nimpy-0.2.0/nimpy.nim(435) $
SIGSEGV: Illegal storage access. (Attempt to read from nil?)

Why it is happening? And how can I fix it? Also, I wanted to be able to catch different python exceptions like I was doing it in Python, but nimpy only raise one type of Exception for all cases, is there anything I can do about it?

M-MAD-Official avatar Apr 10 '22 18:04 M-MAD-Official

I think I figured it out; I needed to assign output to py.None when initializing. But I think it is necessary to do something in nimpy so we won't need to initialize PyObject variables. And I still could not figure out how to catch different Python Exceptions

M-MAD-Official avatar Apr 11 '22 03:04 M-MAD-Official