wlav / cppyy

Other
391 stars 40 forks source link

No way to disable ANSI control sequences in exceptions (colored diagnostics) #108

Open torokati44 opened 1 year ago

torokati44 commented 1 year ago

Consider this snippet (the rethrowing is just to easily redirect the error message to stdout):

#cppyy_colordiagnostics.py
import cppyy

try:
    cppyy.cppdef("""
    #include "notfound.h"
    """)
except Exception as e:
    print(e)

This prints out an error message like this, as expected:

Failed to parse the given C++ code
input_line_18:2:14: fatal error: 'notfound.h' file not found
    #include "notfound.h"
             ^~~~~~~~~~~~

It even has formatting (bold and colored text) in my terminal, which is great... ... unless the error message is supposed to appear anywhere else that is not a TTY, for example: kdialog --msgbox "$(python3 cppyy_colordiagnostics.py)"

This results in:

image

As far as I'm aware, one way to disable those escape sequences is by setting this environment variable: EXTRA_CLING_ARGS="-fno-color-diagnostics" python3 cppyy_colordiagnostics.py

However, this does nothing, the output is still colored.

Even though the flags added here do get to clang somehow, because it complains on unknown flags: EXTRA_CLING_ARGS="-fno-coder-diagnocchis" python3 cppyy_colordiagnostics.py This also prints: error: unknown argument: '-fno-coder-diagnocchis'

wlav commented 1 year ago

From what I can tell, Cling has hard-coded the enabling of coloring (and is using its own scheme). I've asked upstream whether there's a way of disabling it.

wlav commented 1 year ago

Yes, upstream confirms the hard-codedness. :(

I'll have to patch in in cppyy-backend. If you care about it right now, it's in CIFactory.cpp, line 1432. Or here, from upstream: https://github.com/root-project/root/blob/master/interpreter/cling/lib/Interpreter/CIFactory.cpp#L1414