0

I write C source code like this, aiming to accelerate the Python code with C, so I include some py object and func in C:

  • include header file Python.h and dynamic link file libpython3.8.so.
// test.h
#include "Python.h"
#include <stdio.h>

// test.c
void main(){
    PyObject *bigrams1 = PySet_New(0);
    
    ret = PySet_Add(bigrams, PyUnicode_FromWideChar(L"nc", 2));
    ret = PySet_Add(bigrams, PyUnicode_FromWideChar(L"ckd", 3));
    ret = PySet_Add(bigrams, PyUnicode_FromWideChar(L"nc.3e", 5));
    // and I check the following if the nc string in in bigrams:
    ret = PySet_Contains(bigram, PyUnicode_FromWideChar(L"nc", 2));

}
  • In this code I create a Python set object and add three string to it, 'nc', 'ckd', 'nc.3e'. The strings added to the set are random, this is not the key point of the problem.
  • and when I check if 'nc' in bigram, which correspond to the PySet_Contains func, the running code corrupts.
./test: line 10: 2976044 Segmentation fault      

But when I change the nc to other strings like anyone else 'ok', 'ckd', 'project', the code was running correctly. Only the nc caused the bug.

What is the problem with the code and the strange string nc?

Additional description

  • I tried both Python3.8 and Python3.11 and the results are the same.
  • I tried thousands of other strings like '中国', 'few', '$$####', '1e32e2', all the other strings work well.
  • That's the strange string 'nc'.
dongrixinyu
  • 172
  • 2
  • 14
  • `ret` is not defined. It's `int main()` not `void main()`. How do you compile your program? – KamilCuk Jan 20 '22 at 09:23
  • Please remove either the "c" or "c++" tag. Also, provide a [mcve] along with e.g. the compiler commandline and the output it produces. – Ulrich Eckhardt Jan 20 '22 at 09:59
  • The dupe target might not be the best. You don’t initialize the python interpreter, such segfaults will bebte result. – ead Jan 20 '22 at 12:04
  • 1
    The reason for the segfault is that you didn't call ```Py_Initialize();```. Full reference for embedding a python interpreter in a C application here: https://docs.python.org/3/extending/embedding.html – user23952 Jan 20 '22 at 15:20
  • The key point is NOT how to call a Py func in C. the key point IS: the special string `nc` caused the segment fault when calling the PySet_Contains while others like any one else didn't! – dongrixinyu Jan 21 '22 at 01:08
  • But still thank all you guys for your help!!! – dongrixinyu Jan 21 '22 at 01:31

0 Answers0