Pebaz / nimporter

Compile Nim Extensions for Python On Import!
MIT License
824 stars 33 forks source link

IndexError: list index out of range #42

Closed juancarlospaco closed 3 years ago

juancarlospaco commented 3 years ago

This line explodes very frequently, without useful debug information:

  File "/home/juan/.local/lib/python3.9/site-packages/nimporter.py", line 48, in __init__
    nim_module = nim_module.splitlines()[-1]
IndexError: list index out of range

Full traceback

>>> import faster_than_walk
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/juan/.local/lib/python3.9/site-packages/faster_than_walk/__init__.py", line 7, in <module>
    from . faster_than_walk import *
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 982, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 925, in _find_spec
  File "/home/juan/.local/lib/python3.9/site-packages/nimporter.py", line 1150, in find_spec
    return Nimporter.import_nim_code(fullname, path, library=False)
  File "/home/juan/.local/lib/python3.9/site-packages/nimporter.py", line 828, in import_nim_code
    NimCompiler.compile_nim_code(
  File "/home/juan/.local/lib/python3.9/site-packages/nimporter.py", line 588, in compile_nim_code
    raise NimCompileException(errors[0])
  File "/home/juan/.local/lib/python3.9/site-packages/nimporter.py", line 48, in __init__
    nim_module = nim_module.splitlines()[-1]
IndexError: list index out of range

>>> exit()

$ ls /home/juan/.local/lib/python3.9/site-packages/faster_than_walk/
faster_than_walk.nim  faster_than_walk.nim.cfg  __init__.py

$ head /home/juan/.local/lib/python3.9/site-packages/faster_than_walk/faster_than_walk.nim
import os, strutils, nimpy

proc walk*(folderpath: string, extensions: seq[string] = @[""], followlinks : bool = false, yieldfiles: bool = true, debugs: bool = false, check_folders: bool = false): seq[string] {.exportpy.} =
  ## Faster os.walk(), followlinks follows SymLinks, yieldfiles yields files else folders, return 1 list of strings.
  let extused {.noalias.} = create(bool, sizeOf(bool))  # Optimization.
  extused[] = extensions != @[""] and extensions.len > 0
  for item in walkDirRec(folderpath, {if yieldfiles: pcFile else: pcDir}, {if followlinks: pcLinkToDir else: pcDir}, checkDir=check_folders):
    if unlikely(debugs): echo item
    if unlikely(extused[]):

$

It is possible to add some defensive programing just on that line ?, something like a try except and print a useful message, manually checking like if not len(nim_module) > 0: print("ERROR: File not found, Nimporter can not find the nim files") or something.

juancarlospaco commented 3 years ago

I found the problem in the end, it is the .cfg that makes that line fail randomly, sometimes it works, sometimes it fails, if I comment the line of --gc: it always work, but the error messages are not very helpful, for some reason using --gc: with the .nim makes it flaky on import.

Pebaz commented 3 years ago

This is good information! The badly-worded index error you're getting was also fixed here very recently :) https://github.com/Pebaz/nimporter/blob/6fc127b86055bc93f3b52d79da78c2994e765359/nimporter.py#L47