Visual Studio can be configured to invoke a Python script during your build process. If you need to do this, you'll likely want to report errors and warnings in a way that Visual Studio will understand. Here's how.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | # Put this code in your script.
#
# It uses the standard Python warnings framework,
# and installs a custom warning message formatter that
# generates messages Visual Studio will understand.
import warnings
from warnings import warn
def my_formatwarning(message, category, filename, lineno):
""" Return a warning message, formatted for Visual Studio """
return "%s(%i) : warning: %s" % (filename, lineno, message)
# Replace default warning formatter with custom formatter
warnings.formatwarning = my_formatwarning
# Now for some sample code that generates warnings.
# Your code can call warn() anywhere.
def foo():
warn("You called foo(); don't do that")
foo()
|
When Visual Studio runs your script, it redirects the output to the Build window. Any line of output that looks like this: <pre>C:\dev\mydir\myfile.py(1025) : warning: something bad happened</pre> is treated as a warning. It increments the warning count; and if you hit F4, Visual Studio will load the file and move the cursor to the specified line. Python comes with a standard warnings module that does everything you need. All you have to do is change the formatting.