Integration with Python
Python is an interpreted programming language. This means that the Python code is read and run by an intermediate program that is called an interpreter. If we are going to use an external native shared library, it is the interpreter that loads the shared library and makes it available to the Python code. Python has a special framework for loading external shared libraries. It is called ctypes and we are going to use it in this section.
Loading the shared libraries using
ctypes is very straightforward. It only requires loading the library and defining the inputs and output of the functions that are going to be used. The following class wraps the ctypes-related logic and makes it available to our main
Stack class, shown in the upcoming code boxes:
from ctypes import * class value_t(Structure): _fields_ = [("data", c_char_p), ("len", c_int)] class _NativeStack: def __init__(self): self.stackLib = cdll.LoadLibrary( ...