cpython: 94d0e842b9ea (original) (raw)
Mercurial > cpython
changeset 91954:94d0e842b9ea
Issue #18395, #22108: Update embedded Python examples to decode correctly command line parameters: use Py_DecodeLocale() and PyUnicode_DecodeFSDefault(). [#18395]
Victor Stinner victor.stinner@gmail.com | |
---|---|
date | Fri, 01 Aug 2014 12:28:49 +0200 |
parents | 93a798c7f270 |
children | 118d6f49d6d6 |
files | Doc/c-api/init.rst Doc/extending/embedding.rst Doc/extending/extending.rst Doc/includes/run-func.c |
diffstat | 4 files changed, 40 insertions(+), 9 deletions(-)[+] [-] Doc/c-api/init.rst 15 Doc/extending/embedding.rst 20 Doc/extending/extending.rst 12 Doc/includes/run-func.c 2 |
line wrap: on
line diff
--- a/Doc/c-api/init.rst +++ b/Doc/c-api/init.rst @@ -134,6 +134,9 @@ Process-wide parameters change for the duration of the program's execution. No code in the Python interpreter will change the contents of this storage.
.. c:function:: wchar* Py_GetProgramName()
@@ -243,6 +246,9 @@ Process-wide parameters
:data:sys.exec_prefix
to be empty. It is up to the caller to modify these
if required after calling :c:func:Py_Initialize
.
.. c:function:: const char* Py_GetVersion()
@@ -339,6 +345,9 @@ Process-wide parameters
:data:sys.path
, which is the same as prepending the current working
directory ("."
).
- Use :c:func:
Py_DecodeLocale
to decode a bytes string to get a - :c:type:
wchar_*
string. + .. note:: It is recommended that applications embedding the Python interpreter for purposes other than executing a single script pass 0 as updatepath, @@ -363,6 +372,9 @@ Process-wide parameters to 1 unless the :program:python
interpreter was started with the :option:-I
. - Use :c:func:
Py_DecodeLocale
to decode a bytes string to get a - :c:type:
wchar_*
string. + .. versionchanged:: 3.4 The updatepath value depends on :option:-I
.
@@ -377,6 +389,9 @@ Process-wide parameters execution. No code in the Python interpreter will change the contents of this storage.
.. c:function:: w_char* Py_GetPythonHome()
--- 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 */[](#l2.7)
Py_Initialize();[](#l2.8)
PyRun_SimpleString("from time import time,ctime\n"[](#l2.9)
"print('Today is', ctime(time()))\n");[](#l2.10)
Py_Finalize();[](#l2.11)
return 0;[](#l2.12)
wchar_t *program = Py_DecodeLocale(argv[0], NULL);[](#l2.13)
if (program == NULL) {[](#l2.14)
fprintf(stderr, "Fatal error: cannot decode argv[0]\n");[](#l2.15)
exit(1);[](#l2.16)
}[](#l2.17)
Py_SetProgramName(program); /* optional but recommended */[](#l2.18)
Py_Initialize();[](#l2.19)
PyRun_SimpleString("from time import time,ctime\n"[](#l2.20)
"print('Today is', ctime(time()))\n");[](#l2.21)
Py_Finalize();[](#l2.22)
PyMem_RawFree(program);[](#l2.23)
} The :c:func:return 0;[](#l2.24)
Py_SetProgramName
function should be called before @@ -160,7 +166,7 @@ for data conversion between Python and C interesting part with respect to embedding Python starts with :: Py_Initialize();
- pName = PyUnicode_DecodeFSDefault(argv[1]); /* Error checking of pName left out */ pModule = PyImport_Import(pName);
--- a/Doc/extending/extending.rst +++ b/Doc/extending/extending.rst @@ -370,11 +370,17 @@ optionally followed by an import of the int main(int argc, char *argv[]) {
wchar_t *program = Py_DecodeLocale(argv[0], NULL);[](#l3.7)
if (program == NULL) {[](#l3.8)
fprintf(stderr, "Fatal error: cannot decode argv[0]\n");[](#l3.9)
exit(1);[](#l3.10)
}[](#l3.11)
+ /* Add a built-in module, before Py_Initialize / PyImport_AppendInittab("spam", PyInit_spam); / Pass argv[0] to the Python interpreter */
Py_SetProgramName(argv[0]);[](#l3.17)
Py_SetProgramName(program);[](#l3.18)
/* Initialize the Python interpreter. Required. */ Py_Initialize(); @@ -386,6 +392,10 @@ optionally followed by an import of the ...
.. note::
Removing entries from sys.modules
or importing compiled modules into
--- a/Doc/includes/run-func.c +++ b/Doc/includes/run-func.c @@ -13,7 +13,7 @@ main(int argc, char *argv[]) } Py_Initialize();