This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author vstinner
Recipients anthony shaw, vstinner
Date 2019-03-21.09:19:43
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1553159983.84.0.456187894854.issue36386@roundup.psfhosted.org>
In-reply-to
Content
> I'm expecting a "this is not a bug, why would the interpreter not be initialized",

this is not a bug, why would the interpreter not be initialized, as documented at:
https://docs.python.org/dev/c-api/init.html

> but it would be nice to get a friendly error message since this is a public API.
IF so, am also happy to submit a PR with a fix

Python has a *very large* C API. It doesn't seem worth it to me to modify every single Python function to detect when the API is misused.

There is maybe room for some clever tricks for some specific cases. For example, modify PyMem_Malloc and PyObject_Malloc default allocators to have an implementation with calls Py_FatalError() with a clear error message like "Py_Initialize must be called before using the Python C API". I modified Python internals to only use PyMem_RawMalloc during early Python initialization, and we can reconfigure PyMem_Malloc and PyObject_Malloc during Py_Initialize().

But... according to your backtrace, it wouldn't help for your exact use case: call PyUnicode_DecodeFSDefault() before Py_Initialize(). The crash occurs before PyMem_Malloc or PyObject_Malloc is called. I don't see any other clever tricks which would have no impact on performance when the API is properly used.


... I suggest to close this issue.
History
Date User Action Args
2019-03-21 09:19:43vstinnersetrecipients: + vstinner, anthony shaw
2019-03-21 09:19:43vstinnersetmessageid: <1553159983.84.0.456187894854.issue36386@roundup.psfhosted.org>
2019-03-21 09:19:43vstinnerlinkissue36386 messages
2019-03-21 09:19:43vstinnercreate