简体   繁体   中英

argparse does not work with Cython on Windows

I am writing a Windows app. using Python/Cython and compiling using MingW GNU C compiler and an MSYS2 terminal.

The small snippet code below shows the issue:

import argparse

if __name__ == "__main__":

    parser = argparse.ArgumentParser(description='Argparse Cython Test')
    parser.add_argument(
        '--run',
        type=str,
        default='',
        dest="runFile",
        required=False,
        help='Execute this file.')

    args = parser.parse_args()
    print ("args -- ", args)

#endif main

I translate the code into C with:

> cython -3 --embed -o argTest.c argTest.py

(Side note: For some reason the emitted C code generates a main function int wmain () instead of int main() so I do that change using a sed script). And then compile with MingW GCC:

> gcc -v -O3 -I/d/Python3/include/ -L /d/Python3/libs/ -o argTest argTest.c -lpython36 -lm -ldl -Wl,--subsystem,windows

I see this output when I run the executable with the argparse options -- seems to be some unicode characters:

> ./argTest.exe -h                                         
usage: \u3a44\u4d5c\u2079\u7453\u6675\u5c66\u6556\u4378\u646d\u7950\u615c\u6772\u6554\u7473\u652e\u6578\u5200\u433d\u5c3a\u4957\ua4f3\u4bf8\u3ff4\u0d00\u682d\u0200▒\u02b1\ua4f3\u4bf8\u3ff0\u0e00.\u02b1▒\u02b1\ua4b4\u0cf8\u3ff0\u0800 [-h] [--run RUNFILE]
\u3a44\u4d5c\u2079\u7453\u6675\u5c66\u6556\u4378\u646d\u7950\u615c\u6772\u6554\u7473\u652e\u6578\u5200\u433d\u5c3a\u4957\ua4f3\u4bf8\u3ff4\u0d00\u682d\u0200▒\u02b1\ua4f3\u4bf8\u3ff0\u0e00.\u02b1▒\u02b1\ua4b4\u0cf8\u3ff0\u0800: error: unrecognized arguments: \u682d\u0200▒\u02b1\ua4f3\u4bf8\u3ff0\u0e00.\u02b1▒\u02b1\ua4b4\u0cf8\u3ff0\u0800

Without the argparse options, the executable runs fine. My application (using wxPython) runs fine on Windows without using argparse options.

Anyone knows what may be happening here?

Thanks

argparse is working fine, but your mixing Python 3 UTF-8 string and char* . Changing wmain for main means you will be handling you inputs as ASCII. Check answers here about how to enable wmain in MINGW compilers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM