[Python-Dev] Python initialization and embedded Python

Serhiy Storchaka storchaka at gmail.com
Sat Nov 18 10:45:56 EST 2017


18.11.17 16:17, Nick Coghlan пише:
> On 18 November 2017 at 10:01, Victor Stinner <victor.stinner at gmail.com> wrote:
>> I'm writing this email to ask if this change is an issue or not to
>> embedded Python and the Python C API. Is it still possible to call
>> "all" functions of the C API before calling Py_Initialize()?
> 
> It isn't technically permitted to call any of them, unless their
> documentation specifically says that calling them before
> `Py_Initialize` is permitted (and that permission is only given for a
> select few configuration APIs in
> https://docs.python.org/3/c-api/init.html).

The Py_Initialize() is not complete. It mentions only 
Py_SetProgramName(), Py_SetPythonHome() and Py_SetPath(). But in other 
places it is documented that Py_SetStandardStreamEncoding(), 
PyImport_AppendInittab(), PyImport_ExtendInittab() should be called 
before Py_Initialize(). And the embedding examples call 
Py_DecodeLocale() before Py_Initialize(). PyMem_RawMalloc(), 
PyMem_RawFree() and PyInitFrozenExtensions() are called before 
Py_Initialize() in Py_FrozenMain(). Also these functions call 
_PyMem_RawStrdup().

Hence, the minimal set of functions that can be called before 
Py_Initialize() is:

* Py_SetProgramName()
* Py_SetPythonHome()
* Py_SetPath()
* Py_SetStandardStreamEncoding()
* PyImport_AppendInittab()
* PyImport_ExtendInittab()
* Py_DecodeLocale()
* PyMem_RawMalloc()
* PyMem_RawFree()
* PyInitFrozenExtensions()



More information about the Python-Dev mailing list