0

I am trying to run a python program from a C program.

However when I add the line import signal in my .py file I get a segmentation fault.

Using GDB I saw that the line pModule = PyImport_Import(pName); returns a NULL value.

(gdb) print pModule
$1 = (PyObject *) 0x0

When I remove the line import signal in python the module loads correctly. Do you know how to fix this ?

Here is my python code:

import signal

def main(person):
    print("DANS PYTHON")
    return "What's up " + person;

And here is my C code:

#include <stdio.h>
#include <Python.h>

void main(void) {
    Py_Initialize();
    PyObject *pName,*pName2, *pModule, *pFunc, *pArgs, *pValue;
    PySys_SetPath(L".");    
    pName = PyUnicode_FromString((char*)"main");                 
    pModule = PyImport_Import(pName);   
    pFunc = PyObject_GetAttrString(pModule, (char*)"main");      
    pArgs = Py_BuildValue("(s)",(char *)"137912500"); 
    pValue = PyObject_CallObject(pFunc, pArgs);                  
    Py_Finalize();                                               
    return;
}

I am working with Ubuntu 20.04

Thanks in advance for your help

4
  • If the import statement fails, what's the point of the code underneath? Which code is actually the one failing? Please be precise with what you do and also extract a minimal reproducible example. Also, you're not doing any error handling at all, which is questionable. Commented Jan 3, 2022 at 20:55
  • Hi, Thanks you for your return, I edit my question to add more informations Commented Jan 3, 2022 at 21:14
  • PySys_SetPath(L".") - wait, what? Why are you setting sys.path = ['.']? Commented Jan 4, 2022 at 0:17
  • Hi, Without this line I thought my C program would not find my python script. Commented Jan 4, 2022 at 20:48

1 Answer 1

2

I'm on Ubuntu 20.04 also and I have the same problem.

I found that the sys.path is ['.'] when run from a C program. So it search only in the current directory.

I was able to run your script by adding these 2 lines before import signal

import sys
sys.path.append("/usr/lib/python3.8")

import signal

def main(person):
    print("DANS PYTHON")
    return "What's up " + person

To get the folder of the signal module I run :

$ python3
Python 3.8.10 (default, Nov 26 2021, 20:14:08) 
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import signal
>>> signal.__file__
'/usr/lib/python3.8/signal.py'
>>> 

I think that a configuration is missing somewhere but I have no clue about that.

Edit : you can also simply add the path in your C program:

PySys_SetPath(L".:/usr/lib/python3.8");
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for these detailed explanations. By following your instructions my program works whether it's by modifying my python or C program.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.