[Python-checkins] cpython: Issue #18395, #22108: Update embedded Python examples to decode correctly

victor.stinner python-checkins at python.org
Fri Aug 1 12:34:50 CEST 2014


http://hg.python.org/cpython/rev/94d0e842b9ea
changeset:   91954:94d0e842b9ea
user:        Victor Stinner <victor.stinner at gmail.com>
date:        Fri Aug 01 12:28:49 2014 +0200
summary:
  Issue #18395, #22108: Update embedded Python examples to decode correctly
command line parameters: use Py_DecodeLocale() and PyUnicode_DecodeFSDefault().

files:
  Doc/c-api/init.rst          |  15 +++++++++++++++
  Doc/extending/embedding.rst |  20 +++++++++++++-------
  Doc/extending/extending.rst |  12 +++++++++++-
  Doc/includes/run-func.c     |   2 +-
  4 files changed, 40 insertions(+), 9 deletions(-)


diff --git a/Doc/c-api/init.rst b/Doc/c-api/init.rst
--- a/Doc/c-api/init.rst
+++ b/Doc/c-api/init.rst
@@ -134,6 +134,9 @@
    change for the duration of the program's execution.  No code in the Python
    interpreter will change the contents of this storage.
 
+   Use :c:func:`Py_DecodeLocale` to decode a bytes string to get a
+   :c:type:`wchar_*` string.
+
 
 .. c:function:: wchar* Py_GetProgramName()
 
@@ -243,6 +246,9 @@
    :data:`sys.exec_prefix` to be empty.  It is up to the caller to modify these
    if required after calling :c:func:`Py_Initialize`.
 
+   Use :c:func:`Py_DecodeLocale` to decode a bytes string to get a
+   :c:type:`wchar_*` string.
+
 
 .. c:function:: const char* Py_GetVersion()
 
@@ -339,6 +345,9 @@
      :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 @@
    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 @@
    execution.  No code in the Python interpreter will change the contents of
    this storage.
 
+   Use :c:func:`Py_DecodeLocale` to decode a bytes string to get a
+   :c:type:`wchar_*` string.
+
 
 .. c:function:: w_char* Py_GetPythonHome()
 
diff --git a/Doc/extending/embedding.rst b/Doc/extending/embedding.rst
--- a/Doc/extending/embedding.rst
+++ b/Doc/extending/embedding.rst
@@ -58,12 +58,18 @@
    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 @@
 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);
 
diff --git a/Doc/extending/extending.rst b/Doc/extending/extending.rst
--- a/Doc/extending/extending.rst
+++ b/Doc/extending/extending.rst
@@ -370,11 +370,17 @@
    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 @@
 
        ...
 
+       PyMem_RawFree(program);
+       return 0;
+   }
+
 .. note::
 
    Removing entries from ``sys.modules`` or importing compiled modules into
diff --git a/Doc/includes/run-func.c b/Doc/includes/run-func.c
--- a/Doc/includes/run-func.c
+++ b/Doc/includes/run-func.c
@@ -13,7 +13,7 @@
     }
 
     Py_Initialize();
-    pName = PyUnicode_FromString(argv[1]);
+    pName = PyUnicode_DecodeFSDefault(argv[1]);
     /* Error checking of pName left out */
 
     pModule = PyImport_Import(pName);

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list