Last active
January 24, 2026 15:48
-
-
Save xealits/9365b19882ba411711e39e7d723074e4 to your computer and use it in GitHub Desktop.
C hotreload/figwheel in Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python | |
| import ctypes | |
| from ctypes import cdll | |
| cur_x = 10 | |
| libpath = "./some_lib.so" | |
| lib = cdll.LoadLibrary(libpath) | |
| print(f"loaded {lib}") | |
| # https://stackoverflow.com/a/62021495/1420489 | |
| def ctypesCloseLibrary(lib): | |
| dlclose_func = ctypes.CDLL(None).dlclose | |
| dlclose_func.argtypes = [ctypes.c_void_p] | |
| dlclose_func.restype = ctypes.c_int | |
| dlclose_func(lib._handle) | |
| def reloadLibrary(lib): | |
| name = lib._name | |
| ctypesCloseLibrary(lib) | |
| return cdll.LoadLibrary(name) | |
| while True: | |
| inp = input("> ") | |
| inp = inp.strip().upper() | |
| if inp == "X": | |
| x = lib.foo(cur_x) | |
| print(x) | |
| elif inp == "L": | |
| lib = reloadLibrary(lib) | |
| print(f"reloaded {lib}") | |
| else: | |
| print(f"unknown input {inp}") |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Related links:
c++filt <symbol>, usenmfor symbols andnm -Cto demangle, set arguments and return types of the function in ctypes.In a multi-process case, when Python forks processes that fork others that eventually call Cpp code, make GDB follow child processes with
set follow-fork-mode child. Keepset detach-on-fork on.