[Ironpython-users] static analysis tool

Dino Viehland dinov at microsoft.com
Wed Oct 15 01:54:27 CEST 2014


Sorry for the delay...  One thing to note is you won't get any warnings like PyLint provides, but it will let you interrogate the analysis.  Here's the basic code:

            string code = "def f(): pass";
            // create an interpreter instance and an analyzer for that interpreter.
            var factory = new IronPythonInterpreterFactory(ProcessorArchitecture.X86);  // IronPython specific
            //var factory = InterpreterFactoryCreator.CreateAnalysisInterpreterFactory(new Version(2, 7)); // generic CPython interpreter
            var analyzer = new PythonAnalyzer(factory, factory.CreateInterpreter(), "__builtin__");

            // add some code into the interpreter.
            var sourceUnit = new StringReader(code);
            var entry = analyzer.AddModule("foo.py", "foo.py", null);
            using (var parser = Parser.CreateParser(sourceUnit, PythonLanguageVersion.V27, new ParserOptions() { BindReferences = true })) {
                entry.UpdateTree(parser.ParseFile(), null);
            }

            // analyze that code
            entry.Analyze(CancellationToken.None);

            // now you can query the resulting analysis
            entry.Analysis.GetTypesByIndex("f", code.Length);


To reference this you just need to add a reference to Microsoft.PythonTools.Analysis for the core analyzer/interpreter support and Microsoft.PythonTools.IronPython.Interpreter for the IronPython specific interpreter support (which understands all of the .NET namespaces).

This is just analyzing a single file but you can easily expand it to analyze multiple files, pull those files off disk, etc...

Hopefully that can get you started...  From there you can start digging into the analyzer code to extend it to support any additional warnings you want.


From: Ironpython-users [mailto:ironpython-users-bounces+dinov=microsoft.com at python.org] On Behalf Of Joseph Mortensen
Sent: Tuesday, October 14, 2014 3:44 PM
To: ironpython-users at python.org
Subject: Re: [Ironpython-users] static analysis tool

Could anyone link me on how to use PTVS's analyzer?

From: Dino Viehland [mailto:dinov at microsoft.com]
Sent: Tuesday, September 30, 2014 8:12 PM
To: Joseph Mortensen; ironpython-users at python.org<mailto:ironpython-users at python.org>
Subject: RE: static analysis tool

Do you want to create your own static analysis rules or just use an existing checker?  If it's the latter you could use PTVS's analyzer (I can point you to some example code if so).  If it's the former we eventually want to add it to PTVS and when we do that we should pick up IronPython support for free.  We'd also accept it as a contribution ;)

From: Ironpython-users [mailto:ironpython-users-bounces+dinov=microsoft.com at python.org] On Behalf Of Joseph Mortensen
Sent: Tuesday, September 30, 2014 10:29 AM
To: ironpython-users at python.org<mailto:ironpython-users at python.org>
Subject: [Ironpython-users] static analysis tool

I'm trying to static analysis on python code that has very heavy usage of .NET libraries. I've seen pyflakes, pylint, and pychecker as the main tools to do this stuff in python, but I haven't gotten any of them working properly with IronPython in windows.

Are there any recommendations for something that could analyze:

import clr
clr.AddReference("System")
from System import String
stuff = String("my string")
print stuff
stuff.ThisDoesntExist()

without tripping up on the System and String imports and find that "stuff.ThisDoesntExist()" doesn't actually exist?

Thanks,
Joe

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ironpython-users/attachments/20141014/5b2d4e8c/attachment.html>


More information about the Ironpython-users mailing list