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 filelibpython3.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 thePySet_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
andPython3.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'
.