How it works
gilguard is one module of plain Python, a few hundred lines with no dependencies. You don’t need any of this to use it, but if you like knowing how the trick is done, or you want to trust a report before you act on it, read on.
Watching every import
When you import gilguard, it puts a finder at the front of sys.meta_path. That finder is a doorman who never finds anything himself. For each import it asks the rest of sys.meta_path who can load the module, and then, before handing the loader back, it wraps that loader’s create_module and exec_module.
The wrapped methods take a reading of sys._is_gil_enabled() before and after they run. If the GIL was off going in and on coming out, that module turned it on. gilguard builds the report on the spot, while the import is still on the stack and the evidence is still warm.
That timing is what makes the report specific. While an import is in progress, gilguard keeps track of which modules are loading inside which (the import chain), and it walks the Python stack to find the import statements involved. The outermost one outside the import machinery is “Your code”. The innermost is “Imported by”.
The chain is tracked per thread, so imports running at the same time on different threads each get their own chain.
The error is raised in the thread that ran the import. If that’s a worker thread, only that thread unwinds: the main thread keeps going with the GIL on, and the process can still exit 0. gilguard leaves that alone on purpose, since killing the process from inside an import would skip cleanup and surprise most programs. The FAQ has a threading.excepthook recipe if you want any flip to stop the program.
The backstop
Some code loads modules without ever walking past the doorman. importlib.util.module_from_spec() with a hand-built spec is the common case. Those imports never reach the finder.
But CPython always says something when it turns the GIL on. It emits its RuntimeWarning at that exact moment, however the module was loaded. gilguard installs its own warnings.showwarning, recognizes that one warning, and reports from there. Every other warning passes through to the original handler untouched.
When an import goes through the finder, the warning still fires, but gilguard ignores it and lets the wrapper raise a moment later. That gives you a cleaner traceback that ends at your import statement.
Why BaseException
GILEnabledError derives from BaseException, the same tier as KeyboardInterrupt and SystemExit, and for the same reason: it reports something the surrounding code can’t recover from. Optional imports in the wild look like this:
try:
import fastjson as json
except ImportError:
import jsonIf the error were an ImportError, or any Exception, that pattern would catch it, fall back to the pure-Python path, and keep running with the GIL already on. Nothing fixed, nothing reported, and the import that did the damage neatly hidden inside someone else’s except block. Because GILEnabledError sits outside Exception, only code that names it explicitly can catch it.
First flip only
gilguard reports once per process, and that isn’t laziness. Once the GIL is on, nothing turns it off, and no later import can turn it on again, so there’s nothing left to report. It’s also why the scanner needs a separate interpreter for each package.
When it checks the interpreter
At import time, before installing anything, gilguard asks two questions. Is this a free-threaded build? If not, it reports gil-build. Is the GIL already on? If so, it reports already-enabled, with sys.flags.gil to show whether it was forced, plus the third-party extension modules already in sys.modules as suspects. Only after both answers come back clean does it install the finder and the warning hook.