yglukhov / nimpy

Nim - Python bridge
MIT License
1.46k stars 62 forks source link

Python Command Line Arguments Disappearing #296

Closed Kaz-Kawashima closed 10 months ago

Kaz-Kawashima commented 10 months ago

Thank you for providing such a wonderful library.

In some conditions, python command line argutments dissapeare. Is this bug or intended behaviour?


sample.nim

import nimpy

let
    builtins = pyBuiltinsModule()
    PyNoneClass = builtins.None.getattr("__class__")

proc isNone(py: PyObject): bool {.inline.} =
    return builtins.isinstance(py, PyNoneClass).to(bool)

proc convertAxis(x, y: PyObject): (PyObject, PyObject) {.exportpy.} =
    if x.isNone or y.isNone:
        return (nil, nil)
    else:
        let out_x = x.to(float) * 2
        let out_y = y.to(float) * 2
        return (builtins.float(out_x), builtins.float(out_y))   

test.py

import sys
from sample import convertAxis

#test
print(sys.argv) # ['']  arguments are missing.

in_x = float(sys.argv[1]) # Python ERROR
in_y = float(sys.argv[2]) # Python ERROR

out_x, out_y = convertAxis(in_x, in_y)

print (out_x == in_x * 2) #True
print (out_y == in_y * 2) #True

execute

python test.py 1 2

result

['']
Traceback (most recent call last):
  File "/home/kawashima/nim/test.py", line 23, in <module>
    in_x = float(sys.argv[1]) # Python ERROR
IndexError: list index out of range

Using argparse also cause the same error.


This issue does not occur with the following code. But our system using argparse, so we can't use this solution.

from sys import argv  #this line is changed
from sample import convertAxis

print(argv) #['test2.py', '1', '2']   it's OK

in_x = float(argv[1])
in_y = float(argv[2])

out_x, out_y = convertAxis(in_x, in_y)

print (out_x == in_x * 2) #True
print (out_y == in_y * 2) #True

my env


reference

164

294

Kaz-Kawashima commented 10 months ago

I confirmed this issue was fixed. Thank you for quick fix!