[Python-Dev] Python initialization and embedded Python

Nick Coghlan ncoghlan at gmail.com
Wed Nov 22 20:24:54 EST 2017


On 22 November 2017 at 21:12, Victor Stinner <victor.stinner at gmail.com>
wrote:

> 2017-11-22 12:04 GMT+01:00 Antoine Pitrou <solipsis at pitrou.net>:
> > IMHO this really needs a simple solution documented somewhere.  Also,
> > hopefully when you do the wrong thing, you get a clear error message to
> > know how to fix your code?
>
> Right now, calling PyMem_RawMalloc() before calling
> _PyRuntime_Initialize() calls the function at address NULL, so you get
> a segmentation fault.
>
> Documenting the new requirements is part of the discussion, it's one
> option how to fix this issue.
>

My own recommendation is that we add Eric's new test case to the embedding
test suite and just make sure it works:

    wchar_t *program = Py_DecodeLocale("spam", NULL);
    Py_SetProgramName(program);
    Py_Initialize();
    Py_Finalize();
    PyMem_RawFree(program);

It does place some additional constraints on us in terms of handling static
initialization of the allocator state, and ensuring we revert back to that
state in Py_Finalize, but I think it's the only way we're going to be able
to reliably replace all calls to malloc & free with PyMem_RawMalloc and
PyMem_RawFree without causing weird problems.

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20171123/d6128ffe/attachment-0001.html>


More information about the Python-Dev mailing list