fat_flying_pigs
fat_flying_pigs

Reputation: 90

How to suppress specific warning from within python script?

I am getting a specific warning that I can reliably ignore. This warning is likely to be patched eventually, and my goal is to remove the warning from my console (to declutter it, less spam I have to look at).

Specifically, I am attempting to use FolderBrowse() in the Python3 package PySimpleGUI on macOS Mojave. This spits out the following message (on runtime):

objc[2542]: Class FIFinderSyncExtensionHost is implemented in both /System/Library/PrivateFrameworks/FinderKit.framework/Versions/A/FinderKit (0x7fff9408e3d8) and /System/Library/PrivateFrameworks/FileProvider.framework/OverrideBundles/FinderSyncCollaborationFileProviderOverride.bundle/Contents/MacOS/FinderSyncCollaborationFileProviderOverride (0x1073e4f50). One of the two will be used. Which one is undefined.

Again, my intent here is to ignore the above warning, not to fix it myself.

I have seen other python warning suppression questions, such as this one

However, I am not trying to hide a group of warning (eg: DeprecationWarning). Instead I want to only hide the one warning shown above.

edit, code I'm using:

import PySimpleGUI as sg
window_rows = [[sg.InputText(), sg.FolderBrowse()]]
sg.Window('', window_rows).Read()

Upvotes: 1

Views: 3112

Answers (2)

Christopher Barber
Christopher Barber

Reputation: 2678

I find that something like this works for this error:

copied_stderr = 0
try:
    if is_mac:
        # Redirect stderr to /dev/null to hide annoying FIFinderSyncExtensionHost warning
        copied_stderr = os.dup(2)
        devnull = os.open(os.devnull, os.O_WRONLY)
        os.dup2(devnull, 2)
        os.close(devnull)
    result = func(**args)
finally:
    if copied_stderr > 0:
        os.dup2(copied_stderr, 2)
        os.close(copied_stderr)

If you encounter this more than once, you can make a contextmanager that does the redirect.

Upvotes: 1

wovano
wovano

Reputation: 5083

See the section Temporarily Suppressing Warnings in the documentation of the warnings module:

If you are using code that you know will raise a warning, such as a deprecated function, but do not want to see the warning (even when warnings have been explicitly configured via the command line), then it is possible to suppress the warning using the catch_warnings context manager:

import warnings

def fxn():
    warnings.warn("deprecated", DeprecationWarning)

with warnings.catch_warnings():
    warnings.simplefilter("ignore")
    fxn()

While within the context manager all warnings will simply be ignored. This allows you to use known-deprecated code without having to see the warning while not suppressing the warning for other code that might not be aware of its use of deprecated code. (...)

This seems exactly what you need, isn't it?

Upvotes: 0

Related Questions