[File] [PATCH] python/magic.py: avoid TypeError in del at interpreter shutdown

Vincent Mihalkovic vmihalko at redhat.com
Mon Feb 16 08:26:58 UTC 2026


Hi,

Thank you for accepting the patch — I appreciate it.

I noticed that the merged change keeps the conditional guard in close(),
but does not include:
    self._close = _close
That line was intended to cache the magic_close function on the instance.
The current if _close: avoids the traceback, but it still relies on the
module-global _close, which may already be cleared during interpreter
shutdown. Storing the function on the instance ensures it remains available
when __del__ runs.

Would you consider also adding the self._close = _close line?

Best regards,
Vincent

On Tue, Feb 10, 2026 at 1:52 PM Vincent Mihalkovic <vmihalko at redhat.com>
wrote:

> Hi,
>
> I am seeing a TypeError at Python interpreter shutdown when using the
> Python bindings shipped with file (python/magic.py).
>
> MagicDetect.__del__() calls Magic.close() (see
> https://github.com/file/file/blob/master/python/magic.py#L300-L310), which
> in turn calls the module-global _close in Magic.close() (see
> https://github.com/file/file/blob/master/python/magic.py#L130-L136).
>
> During interpreter shutdown, module globals may already be cleared, so
> _close can become None, resulting in: TypeError: ‘NoneType’ object is not
> callable
>
> The attached patch avoids relying on the module-global _close during
> finalization by caching the magic_close function pointer on the Magic instance
> and using that in close(). This prevents the shutdown exception without
> changing normal runtime behavior.
>
> Related downstream report:
> https://bugzilla.redhat.com/show_bug.cgi?id=2419719
>
> Best regards,
> Vincent
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mailman.astron.com/pipermail/file/attachments/20260216/be176c97/attachment.htm>


More information about the File mailing list