Skip to content

Commit 25e014b

Browse files
committed
Issue python#18395, python#22108: Update embedded Python examples to decode correctly
command line parameters: use Py_DecodeLocale() and PyUnicode_DecodeFSDefault().
1 parent f6a271a commit 25e014b

File tree

4 files changed

+40
-9
lines changed

4 files changed

+40
-9
lines changed

Doc/c-api/init.rst

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,9 @@ Process-wide parameters
134134
change for the duration of the program's execution. No code in the Python
135135
interpreter will change the contents of this storage.
136136
137+
Use :c:func:`Py_DecodeLocale` to decode a bytes string to get a
138+
:c:type:`wchar_*` string.
139+
137140
138141
.. c:function:: wchar* Py_GetProgramName()
139142
@@ -243,6 +246,9 @@ Process-wide parameters
243246
:data:`sys.exec_prefix` to be empty. It is up to the caller to modify these
244247
if required after calling :c:func:`Py_Initialize`.
245248
249+
Use :c:func:`Py_DecodeLocale` to decode a bytes string to get a
250+
:c:type:`wchar_*` string.
251+
246252
247253
.. c:function:: const char* Py_GetVersion()
248254
@@ -339,6 +345,9 @@ Process-wide parameters
339345
:data:`sys.path`, which is the same as prepending the current working
340346
directory (``"."``).
341347
348+
Use :c:func:`Py_DecodeLocale` to decode a bytes string to get a
349+
:c:type:`wchar_*` string.
350+
342351
.. note::
343352
It is recommended that applications embedding the Python interpreter
344353
for purposes other than executing a single script pass 0 as *updatepath*,
@@ -363,6 +372,9 @@ Process-wide parameters
363372
to 1 unless the :program:`python` interpreter was started with the
364373
:option:`-I`.
365374
375+
Use :c:func:`Py_DecodeLocale` to decode a bytes string to get a
376+
:c:type:`wchar_*` string.
377+
366378
.. versionchanged:: 3.4 The *updatepath* value depends on :option:`-I`.
367379
368380
@@ -377,6 +389,9 @@ Process-wide parameters
377389
execution. No code in the Python interpreter will change the contents of
378390
this storage.
379391
392+
Use :c:func:`Py_DecodeLocale` to decode a bytes string to get a
393+
:c:type:`wchar_*` string.
394+
380395
381396
.. c:function:: w_char* Py_GetPythonHome()
382397

Doc/extending/embedding.rst

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,18 @@ perform some operation on a file. ::
5858
int
5959
main(int argc, char *argv[])
6060
{
61-
Py_SetProgramName(argv[0]); /* optional but recommended */
62-
Py_Initialize();
63-
PyRun_SimpleString("from time import time,ctime\n"
64-
"print('Today is', ctime(time()))\n");
65-
Py_Finalize();
66-
return 0;
61+
wchar_t *program = Py_DecodeLocale(argv[0], NULL);
62+
if (program == NULL) {
63+
fprintf(stderr, "Fatal error: cannot decode argv[0]\n");
64+
exit(1);
65+
}
66+
Py_SetProgramName(program); /* optional but recommended */
67+
Py_Initialize();
68+
PyRun_SimpleString("from time import time,ctime\n"
69+
"print('Today is', ctime(time()))\n");
70+
Py_Finalize();
71+
PyMem_RawFree(program);
72+
return 0;
6773
}
6874

6975
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
160166
interesting part with respect to embedding Python starts with ::
161167

162168
Py_Initialize();
163-
pName = PyUnicode_FromString(argv[1]);
169+
pName = PyUnicode_DecodeFSDefault(argv[1]);
164170
/* Error checking of pName left out */
165171
pModule = PyImport_Import(pName);
166172

Doc/extending/extending.rst

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,11 +370,17 @@ optionally followed by an import of the module::
370370
int
371371
main(int argc, char *argv[])
372372
{
373+
wchar_t *program = Py_DecodeLocale(argv[0], NULL);
374+
if (program == NULL) {
375+
fprintf(stderr, "Fatal error: cannot decode argv[0]\n");
376+
exit(1);
377+
}
378+
373379
/* Add a built-in module, before Py_Initialize */
374380
PyImport_AppendInittab("spam", PyInit_spam);
375381

376382
/* Pass argv[0] to the Python interpreter */
377-
Py_SetProgramName(argv[0]);
383+
Py_SetProgramName(program);
378384

379385
/* Initialize the Python interpreter. Required. */
380386
Py_Initialize();
@@ -386,6 +392,10 @@ optionally followed by an import of the module::
386392

387393
...
388394

395+
PyMem_RawFree(program);
396+
return 0;
397+
}
398+
389399
.. note::
390400

391401
Removing entries from ``sys.modules`` or importing compiled modules into

Doc/includes/run-func.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ main(int argc, char *argv[])
1313
}
1414

1515
Py_Initialize();
16-
pName = PyUnicode_FromString(argv[1]);
16+
pName = PyUnicode_DecodeFSDefault(argv[1]);
1717
/* Error checking of pName left out */
1818

1919
pModule = PyImport_Import(pName);

0 commit comments

Comments
 (0)