I was trying to follow this example to attempt to create a simple hash table in C that I could use in Python (using CFFI). Following are the corresponding files and relevant code.
hash_table.h
:
typedef struct {
size_t size;
...
int cmp_function(const PyObject*, const PyObject*);
} hash_table_t;
build_hash_table.py
:
from cffi import cffi
HEADER_FILE_NAME = "hash_table.h"
SOURCE_FILE_NAME = "hash_table.c"
ffi = FFI()
header = open(HEADER_FILE_NAME, "rt").read()
source = open(SOURCE_FILE_NAME, "rt").read()
ffi.set_source("hashtable.hash_table", header + source)
ffi.cdef(header)
if __name__ = "__main__":
ffi.compile()
Now, everything so far works. However, I am going to want to insert whatever data types in the hash table. How do I get access to whatever object I am receiving within the C code? For example, say I had this class in Python:
class Person():
def __init__(self, name, id):
self.name = name
self.id = id
def __eq__(self, other):
return self.id == other.id
When I am searching for Person
objects in the hash table, how do I access the __eq__
method? As you can see above, I have a generic compare function declared as int cmp_function(const void*, const void*);
in the hash table. The goal would be that every time I search for an object, I can know the __eq__
method definition of that object - in the C side.
Hope I made my question clear, thank you in advance!