codon icon indicating copy to clipboard operation
codon copied to clipboard

list index out of range

Open cyw3 opened this issue 3 years ago • 4 comments

test.py

import argparse

class Test(object):
    def __init__(self):
        print("Test---------\n")
        self.argparser = argparse.ArgumentParser()

test.codon


from python import test

if __name__ == "__main__":
    test.Test()

I have the demo above, and run it with codon, and i get the error PyError: list index out of range

$ codon run test.codon
Test---------

PyError: list index out of range

Raised from: pyobj.exc_check:0
/data/test/codon-deploy/lib/codon/stdlib/internal/python.codon:623:13

Backtrace:
  [0x7fc1ad3e4af3] GCC_except_table171 at /data/test/codon-deploy/lib/codon/stdlib/internal/python.codon:623:13
  [0x7fc1ad3e4b20] GCC_except_table171 at /data/test/codon-deploy/lib/codon/stdlib/internal/python.codon:626:9
  [0x7fc1ad3e5688] GCC_except_table171 at /data/test/codon-deploy/lib/codon/stdlib/internal/python.codon:650:68
  [0x7fc1ad3e6915] GCC_except_table171 at /workspace/codon.py:14:5
Aborted (core dumped)

cyw3 avatar Dec 15 '22 07:12 cyw3

It may be because of sys.argv in argparse.ArgumentParser.__init__().

test.py

import sys

class Test(object):
    def __init__(self):
        print("python: ", sys.argv)

test.codon


from python import test

import sys

if __name__ == "__main__":
    test.Test()
    print("codon: ", sys.argv)
$ codon run test.codon
python:  []
codon:  ['hades.codon']

How can i use codon to call python module argparse.ArgumentParser?

cyw3 avatar Dec 15 '22 09:12 cyw3

Hi @cyw3 -- here's an example of how this can be done (I just took the simple example from the argparse docs):

# .codon file
import sys
from python import argparse

parser = argparse.ArgumentParser(
                    prog = 'ProgramName',
                    description = 'What the program does',
                    epilog = 'Text at the bottom of help')

parser.add_argument('filename')           # positional argument
parser.add_argument('-c', '--count')      # option that takes a value
parser.add_argument('-v', '--verbose',
                    action='store_true')  # on/off flag

args = parser.parse_args(sys.argv[1:])
print(args.filename, args.count, args.verbose)

Just remember to set the CODON_PYTHON env variable to the Python library as described here.

arshajii avatar Dec 15 '22 14:12 arshajii

Hi @cyw3 -- here's an example of how this can be done (I just took the simple example from the argparse docs):

# .codon file
import sys
from python import argparse

parser = argparse.ArgumentParser(
                    prog = 'ProgramName',
                    description = 'What the program does',
                    epilog = 'Text at the bottom of help')

parser.add_argument('filename')           # positional argument
parser.add_argument('-c', '--count')      # option that takes a value
parser.add_argument('-v', '--verbose',
                    action='store_true')  # on/off flag

args = parser.parse_args(sys.argv[1:])
print(args.filename, args.count, args.verbose)

Just remember to set the CODON_PYTHON env variable to the Python library as described here.

When I run the demo you provided, I get the error PyError: 0:

$ codon run  test.codon
usage: ProgramName [-h] [-c COUNT] [-v] filename

What the program does

positional arguments:
  filename

optional arguments:
  -h, --help            show this help message and exit
  -c COUNT, --count COUNT
  -v, --verbose

Text at the bottom of help
PyError: 0

Raised from: pyobj.exc_check:0
/data/test/codon-deploy/lib/codon/stdlib/internal/python.codon:623:13

Backtrace:
  [0x7f0dff8c3f03] GCC_except_table255 at /data/test/codon-deploy/lib/codon/stdlib/internal/python.codon:623:13
  [0x7f0dff8c3f30] GCC_except_table255 at /data/test/codon-deploy/lib/codon/stdlib/internal/python.codon:626:9
  [0x7f0dff8c6192] GCC_except_table255 at /data/test/codon-deploy/lib/codon/stdlib/internal/python.codon:650:68
  [0x7f0dff8c782c] GCC_except_table255 at /workspace/test.codon:30:8
Aborted (core dumped)

cyw3 avatar Dec 16 '22 02:12 cyw3

It happens because filename is not specified.

This works:

codon run argparse_python.codon -c 5 -v blabla.txt
['-c', '5', '-v', 'blabla.txt']

ghuls avatar Dec 16 '22 08:12 ghuls

Closing this for now but please let me know if you have any other questions about this.

arshajii avatar Dec 18 '22 22:12 arshajii