[File] [PATCH] python/magic.py: avoid TypeError in del at interpreter shutdown
Christos Zoulas
christos at zoulas.com
Mon Feb 16 14:19:03 UTC 2026
Done!
christos
> On Feb 16, 2026, at 3:26 AM, Vincent Mihalkovic <vmihalko at redhat.com> wrote:
>
> 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 <mailto: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
>>
>>
>
> --
> File mailing list
> File at astron.com
> https://mailman.astron.com/mailman/listinfo/file
> <sanitizer.log>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mailman.astron.com/pipermail/file/attachments/20260216/15c651c6/attachment.htm>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 235 bytes
Desc: Message signed with OpenPGP
URL: <https://mailman.astron.com/pipermail/file/attachments/20260216/15c651c6/attachment.asc>
More information about the File
mailing list