[issue36963] PyDict_GetItem SegFaults on simple dictionary lookup when using Ctypes

Apoorv Reddy report at bugs.python.org
Sun May 19 10:22:10 EDT 2019


Apoorv Reddy <apoorvreddy19 at gmail.com> added the comment:

Greetings Larry !

I apologize for spamming so many people. I was hoping to get some insight into this ! Could you let me know to whom I could reach out for help ? I've included tehybel as I saw that he has raised/resolved some issues with PyDict in the past.

My C code is essentially just this:

#include "Python.h"

#ifdef __cplusplus
extern "C" double test(PyObject* key, PyObject* dict)
#else
double test(PyObject* key, PyObject* dict)
#endif
{
	PyObject* list = PyObject_GetItem(dict, key);
	return 0.0;
}

------------

And my Python Code is this. I'm pretty sure I've got the input and output types correct here.

from ctypes import cdll
from ctypes import *
import json
import sys
import pickle


dll = CDLL('./test2.so')
dll.test.restype = c_double
dll.test.argtypes = (py_object, py_object)

d = {68113113140: [1, 2]}
for i in d.keys():
	if i == 68113113140:
		break
print(dll.test(i, d)) # this works just fine
print(dll.test(68113113140, d) # this segfaults !

GDB shows me that PyObject_RichCompare (called inside PyDict_GetItem) is the function where the segmentation fault happens !
--------

Hoping for some guidance here ! I've been trying to resolve this for 3 days now. I have made sure that I've compiled with the correct version of Python headers and that I'm using the same version of interpreter as well (in this case Python 3.5.6)

----------
nosy:  -amaury.forgeotdarc, belopolsky, benjamin.peterson, christian.heimes, duaneg, ebarry, georg.brandl, inada.naoki, meador.inge, ned.deily, rhettinger, serhiy.storchaka

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue36963>
_______________________________________


More information about the Python-bugs-list mailing list