diff options
author | Victor Stinner <victor.stinner@gmail.com> | 2014-08-01 10:28:49 (GMT) |
---|---|---|
committer | Victor Stinner <victor.stinner@gmail.com> | 2014-08-01 10:28:49 (GMT) |
commit | 25e014bd91e2f64b3231ef2bf5ddac8bb99ed637 (patch) | |
tree | 117d491c5ed3f749eadee5a6e457371acac81318 /Doc/extending/embedding.rst | |
parent | f6a271ae980d2f3fb450f745b8f87624378156c4 (diff) | |
download | cpython-25e014bd91e2f64b3231ef2bf5ddac8bb99ed637.zip cpython-25e014bd91e2f64b3231ef2bf5ddac8bb99ed637.tar.gz cpython-25e014bd91e2f64b3231ef2bf5ddac8bb99ed637.tar.bz2 |
Issue #18395, #22108: Update embedded Python examples to decode correctly
command line parameters: use Py_DecodeLocale() and PyUnicode_DecodeFSDefault().
Diffstat (limited to 'Doc/extending/embedding.rst')
-rw-r--r-- | Doc/extending/embedding.rst | 20 |
1 files changed, 13 insertions, 7 deletions
diff --git a/Doc/extending/embedding.rst b/Doc/extending/embedding.rst index 6cb686a..acd60ae 100644 --- a/Doc/extending/embedding.rst +++ b/Doc/extending/embedding.rst @@ -58,12 +58,18 @@ perform some operation on a file. :: int main(int argc, char *argv[]) { - Py_SetProgramName(argv[0]); /* optional but recommended */ - Py_Initialize(); - PyRun_SimpleString("from time import time,ctime\n" - "print('Today is', ctime(time()))\n"); - Py_Finalize(); - return 0; + wchar_t *program = Py_DecodeLocale(argv[0], NULL); + if (program == NULL) { + fprintf(stderr, "Fatal error: cannot decode argv[0]\n"); + exit(1); + } + Py_SetProgramName(program); /* optional but recommended */ + Py_Initialize(); + PyRun_SimpleString("from time import time,ctime\n" + "print('Today is', ctime(time()))\n"); + Py_Finalize(); + PyMem_RawFree(program); + return 0; } The :c:func:`Py_SetProgramName` function should be called before @@ -160,7 +166,7 @@ for data conversion between Python and C, and for error reporting. The interesting part with respect to embedding Python starts with :: Py_Initialize(); - pName = PyUnicode_FromString(argv[1]); + pName = PyUnicode_DecodeFSDefault(argv[1]); /* Error checking of pName left out */ pModule = PyImport_Import(pName); |