How to suppress warning output ?

I built a DXF file converter application using KLayout python module. It works quite well. But it produces warning messages saying "Warning: Entity VIEWPORT not supported - ignored.". And I just want to ignore and hide them from users. Is there any way to suppress these warning messages without re-building python module?

Comments

  • No, sorry. Currently these warnings are unconditionally sent to stdout for the Python module. The only way to suppress them is to redirect stdout (temporarily) somewhere else.

    But I'd say these warnings are an indication that DXF is not fully implemented. Please take this message seriously. DXF is not well defined and KLayout's module implements a subset only and even for that I'm not sure about the quality. Unless you know what you're doing, I do not advise to use it.

    Matthias

  • Thanks for detailed reply. I tried to redirect stdout without success. Using contextlib, I can only suppress print("readFile"). Viewport-related warning messages are NOT suppressed. Strange...

    import pya
    from contextlib import contextmanager
    import sys, os
    
    @contextmanager
    def suppress_stdout():
        with open(os.devnull, "w") as devnull:
            old_stdout = sys.stdout
            sys.stdout = devnull
            try:  
                yield
            finally:
                sys.stdout = old_stdout
    
    def readFile():
        print("readFile")
        layout = pya.Layout()
        load_options = pya.LoadLayoutOptions()
        load_options.dxf_polyline_mode=3
        load_options.dxf_text_scaling=100
        load_options.dxf_circle_points=64
        load_options.dxf_dbu=0.01
        load_options.dxf_unit=1
        layout.read("sample.dxf", load_options)
    
    with suppress_stdout():
        readFile()
    
Sign In or Register to comment.