I don't know how to instruct ctypes how to unload a library (didn't find a way on [Python.Docs]: ctypes - A foreign function library for Python, but that doesn't mean that there isn't one).
It can be done manually, by forcing the loader to (decrement the library's reference count and) unload it via [Man7]: DLCLOSE(3P) (also read [Man7]: DLOPEN(3) for additional info on loading / unloading libraries).
dll00.c:
#include <stdio.h>
int func0(int arg0)
{
int alter_factor = 2;
printf("From C - arg0: %d, alter_factor: %d\n", arg0, alter_factor);
return arg0 * alter_factor;
}
code00.py:
#!/usr/bin/env python
import ctypes as cts
import sys
DLL_NAME = "./dll00.{:s}".format("dll" if sys.platform[:3].lower() == "win" else "so")
def handle_dll(name=DLL_NAME):
dll = cts.CDLL(name)
func0 = dll.func0
func0.argtypes = (cts.c_int,)
func0.restype = cts.c_int
return dll, func0
def main(*argv):
dlclose_func = cts.CDLL(None).dlclose
dlclose_func.argtypes = (cts.c_void_p,)
dlclose_func.restype = cts.c_int
dll, func0 = handle_dll()
res = func0(42)
print(res)
dlclose_func(dll._handle)
input("In another terminal, modify the C code (e.g. change `alter_factor`), recompile (gcc -fPIC -shared -o dll00.so dll00.c), and when done press ENTER here... ")
dll, func0 = handle_dll()
res = func0(42)
print(res)
if __name__ == "__main__":
print("Python {:s} {:03d}bit on {:s}\n".format(" ".join(elem.strip() for elem in sys.version.split("\n")),
64 if sys.maxsize > 0x100000000 else 32, sys.platform))
rc = main(*sys.argv[1:])
print("\nDone.\n")
sys.exit(rc)
Output:
(qaic-env) [cfati@cfati-5510-0:/mnt/e/Work/Dev/StackOverflow/q050964033]> ~/sopr.sh
### Set shorter prompt to better fit when pasted in StackOverflow (or other) pages ###
[064bit prompt]> ls
code00.py dll00.c dll00.so
[064bit prompt]>
[064bit prompt]> python code00.py
Python 3.8.10 (default, Nov 26 2021, 20:14:08) [GCC 9.3.0] 064bit on linux
From C - arg0: 42, alter_factor: 2
84
In another terminal, modify the C code (e.g. change `alter_factor`), recompile (gcc -fPIC -shared -o dll00.so dll00.c), and when done press ENTER here...
From C - arg0: 42, alter_factor: 3
126
Done.
Related (more or less):