1

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

user0
  • 97
  • 9
  • do you change only the name `wmain`-> `main` or also the type of parameter argv from `wchar_t**`->`char**`? – ead Aug 20 '18 at 04:43
  • I have changed `int wmain(int argc, wchar_t **argv) ` to `int main(int argc, wchar_t **argv)` in the `#elif defined(WIN32) || defined(MS_WINDOWS)` clause – user0 Aug 20 '18 at 07:36
  • I don't know Windows well enough, but probably you have also to change wchar_t to char, otherwise you are probably reinterpreting ascii as unicode. – ead Aug 20 '18 at 07:45
  • Thanks for the tip. I tried that -- now it doesn't print anything at all. I see warnings because Python3 libs expect wchar_t parameters. – user0 Aug 20 '18 at 14:49

1 Answers1

0

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.

yorodm
  • 4,359
  • 24
  • 32
  • Thanks for the link. argparse is working fine after switching to the 64-bit MinGW and compiling with -municode (with target i686-w64-mingw32). No mode to Cython output is required. – user0 Aug 30 '18 at 03:33