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

Vincent Mihalkovic vmihalko at redhat.com
Fri Feb 20 18:02:59 UTC 2026


Hi,
Thanks again for adding the _close caching change.

It seems the inserted line is indented with a tab, while the rest of the
file uses spaces. Right now it looks like this:
class Magic(object):
    def __init__(self, ms):
        self._magic_t = ms
    self._close = _close
Note the tab before self._close, which (python3 magic.py) leads to:
    TabError: inconsistent use of tabs and spaces in indentation


Could you replace the tab with 4 spaces to match the surrounding
indentation?
Thanks for taking a look into it again.


Best regards,
Vincent

On Mon, Feb 16, 2026 at 3:25 PM Christos Zoulas <christos at zoulas.com> wrote:

> 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>
> 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/20260220/6947b7ea/attachment.htm>


More information about the File mailing list