[Python-porting] main() -> Py_SetProgramName()

Barry Warsaw barry at python.org
Wed Dec 5 01:12:01 CET 2012


One gotcha with porting embedded Python 3 is the mismatch between main()'s
signature and Py_SetProgramName() and PySys_SetArgv().

In Python 2, everything was easy.  You got char*'s from main() and could pass
them directly to these two calls.  Not in Python 3, because they now take
wchar_t*'s instead.  I get why these signatures have changed, but that doesn't
make life very easy for porters.

Take a look at main() in Modules/python.c to see the headaches Python itself
goes through do the conversions.  I think we're doing a disservice to
embedders not to provide convenience functions, alternative APIs, or at the
very least code examples for helping them do the argument conversions.  This
is not easy code, it's error prone, and folks shouldn't have to roll their own
every time they need to do this.

Using the algorithm in main() is probably not the best recommendation either,
because it uses non-public API methods such as _Py_char2wchar().  Perhaps
these should be promoted to a public method, or we should add a method to get
from main()'s char** to a wchar_t**.

For now, I've tried to use mbsrtowcs(), though I haven't done extensive
testing on the code.  I think Python ultimately uses mbstowcs() down deep in
its bowels.

There was some discussion of this back in 2009 IIRC, but nothing ever came of
it.  I think MvL at the time was against adding any convenience or alternative
API to Python.

Has anybody else encountered this while porting embedded Python applications
to Python 3?  How did you solve it?

I'm happy to bring this up on python-dev, but I also don't want to have to
wait until Python 3.4 to have a nice solution.

Cheers,
-Barry
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 836 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-porting/attachments/20121204/634f9c74/attachment.pgp>


More information about the Python-porting mailing list