mirror of
https://github.com/python/cpython.git
synced 2025-08-03 08:34:29 +00:00
command line parameters: use Py_DecodeLocale() and PyUnicode_DecodeFSDefault().
This commit is contained in:
parent
f6a271ae98
commit
25e014bd91
4 changed files with 40 additions and 9 deletions
|
@ -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);
|
||||
|
||||
|
|
|
@ -370,11 +370,17 @@ optionally followed by an import of the module::
|
|||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
wchar_t *program = Py_DecodeLocale(argv[0], NULL);
|
||||
if (program == NULL) {
|
||||
fprintf(stderr, "Fatal error: cannot decode argv[0]\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
/* Add a built-in module, before Py_Initialize */
|
||||
PyImport_AppendInittab("spam", PyInit_spam);
|
||||
|
||||
/* Pass argv[0] to the Python interpreter */
|
||||
Py_SetProgramName(argv[0]);
|
||||
Py_SetProgramName(program);
|
||||
|
||||
/* Initialize the Python interpreter. Required. */
|
||||
Py_Initialize();
|
||||
|
@ -386,6 +392,10 @@ optionally followed by an import of the module::
|
|||
|
||||
...
|
||||
|
||||
PyMem_RawFree(program);
|
||||
return 0;
|
||||
}
|
||||
|
||||
.. note::
|
||||
|
||||
Removing entries from ``sys.modules`` or importing compiled modules into
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue