#๐Ÿ”’ Embedding CPython: Interacting with module seems to trigger segment fault

9 messages ยท Page 1 of 1 (latest)

brazen urchin
#

I'm trying to embed CPython 3.13.9 into my program using Zig. The idea is that I'll import a Python module using PyImport_ImportModule and hold on to it. And later PyObject_GetAttrString and PyObject_CallObject will be used to call functions in the module.

But segment fault occurs when I call PyObject_GetAttrString, and sometimes PyObject_CallObject too (I'm unsure about this because I did changed some stuff when debugging, but I can't recall any changes that might relate to this happening). I'm sure that I didn't call PY_DECREF on the module nor Py_Finalize before trying to call PyObject_GetAttrString and PyObject_CallObject.

I needed to call multiple functions in Python from Zig, at some point I got it to work with the first function call. But later on it somehow stopped working again, and will always crash on the second PyObject_GetAttrString call (segment fault at address 0x0).

Even after hours of research, back and forth with ChatGPT, I still can't figure out why this is happening. Am I miss understand how object or module lifetime work? Any help is appreciated!

proud wigeonBOT
#

@brazen urchin

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

brazen urchin
#

Embedding Python: Interacting with module seems to trigger segment fault

brazen urchin
#

Embedding CPython: Interacting with module seems to trigger segment fault

minor falcon
#

i'd first check whether the module is null
would be good if you provided a minimal reproducible example

brazen urchin
#

As for the minimal example I'll need to do more testing and that might take some time.

proud wigeonBOT
#
Python help channel closed for inactivity

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.