invoking pylint programmatically

后端 未结 7 855
难免孤独
难免孤独 2020-12-05 00:21

I\'d like to invoke the pylint checker, limited to the Error signalling part, as part of my unit testing. so I checked the pylint executable script, got to the pylint

7条回答
  •  醉梦人生
    2020-12-05 00:56

    Instead of creating a WritableObject class we can use StringIO. StringIO contains write method.

    import sys
    try:
        from io import StringIO
    except:
        from StringIO import StringIO
    
    stdout = sys.stdout
    sys.stdout = StringIO()
    
    ARGS = ["-r","n", "--rcfile=rcpylint"]
    r = lint.Run(['../test.py']+ARGS, exit=False)
    
    test = sys.stdout.getvalue()
    sys.stdout.close()
    sys.stdout = stdout
    
    print (test.split('\n'))
    

    Source:

    • @cdarke 's answer

    • @mad7777's answer

提交回复
热议问题