コード例 #1
0
ファイル: ImportCoverageCommand.cs プロジェクト: zooba/PTVS
        private void ConvertCoveragePy(string inputFile, string outputFile, PythonLanguageVersion? version) {
            var baseDir = Path.GetDirectoryName(inputFile);
            using (FileStream tmp = new FileStream(inputFile, FileMode.Open))
            using (FileStream outp = new FileStream(outputFile, FileMode.Create)) {
                // Read in the data from coverage.py's XML file
                CoverageFileInfo[] fileInfo = new CoveragePyConverter(baseDir, tmp).Parse();

                // Discover what version we should use for this if one hasn't been provided...
                if (version == null) {
                    foreach (var file in fileInfo) {
                        var project = _serviceProvider.GetProjectFromFile(file.Filename);
                        if (project != null) {
                            version = project.ActiveInterpreter.Configuration.Version.ToLanguageVersion();
                            break;
                        }
                    }
                }

                if (version == null) {
                    var interpreters = _serviceProvider.GetComponentModel().GetService<IInterpreterOptionsService>();
                    version = interpreters?.DefaultInterpreter.Configuration.Version.ToLanguageVersion()
                        ?? PythonLanguageVersion.None;
                }

                // Convert that into offsets within the actual code
                var covInfo = Import(fileInfo, version.Value);

                // Then export as .coveragexml
                new CoverageExporter(outp, covInfo).Export();
            }
        }
コード例 #2
0
ファイル: PythonKeywords.cs プロジェクト: omnimark/PTVS
 /// <summary>
 /// Returns a sequence of all keywords usable in an expression in a
 /// particular version of Python.
 /// </summary>
 public static IEnumerable<string> Expression(PythonLanguageVersion version = PythonLanguageVersion.None) {
     yield return "and";
     yield return "as";
     if (version.IsNone() || version >= PythonLanguageVersion.V35) {
         yield return "await";
     }
     yield return "else";
     if (version.IsNone() || version.Is3x()) {
         yield return "False";
     }
     yield return "for";
     if (version.IsNone() || version >= PythonLanguageVersion.V33) {
         yield return "from";
     }
     yield return "if";
     yield return "in";
     yield return "is";
     yield return "lambda";
     yield return "None";
     yield return "not";
     yield return "or";
     if (version.IsNone() || version.Is3x()) {
         yield return "True";
     }
     yield return "yield";
 }
コード例 #3
0
ファイル: PythonPaths.cs プロジェクト: wenh123/PTVS
        private static PythonVersion GetCPythonVersion(PythonLanguageVersion version) {
            foreach (var baseKey in new[] { Registry.LocalMachine, Registry.CurrentUser }) {
                using (var python = baseKey.OpenSubKey(PythonCorePath)) {
                    var res = TryGetCPythonPath(version, python);
                    if (res != null) {
                        return res;
                    }
                }
            }

            if (Environment.Is64BitOperatingSystem) {
                foreach (var baseHive in new[] { RegistryHive.LocalMachine, RegistryHive.CurrentUser }) {
                    var python64 = RegistryKey.OpenBaseKey(baseHive, RegistryView.Registry64).OpenSubKey(PythonCorePath);
                    var res = TryGetCPythonPath(version, python64);
                    if (res != null) {
                        return res;
                    }
                }
            }

            var path = "C:\\Python" + version.ToString().Substring(1) + "\\python.exe";
            var arch = Microsoft.PythonTools.Analysis.NativeMethods.GetBinaryType(path);
            if (arch == ProcessorArchitecture.X86) {
                return new PythonVersion(path, version, CPythonGuid);
            } else if (arch == ProcessorArchitecture.Amd64) {
                return new PythonVersion(path, version, CPython64Guid);
            } else {
                return null;
            }
        }
コード例 #4
0
ファイル: PythonAst.cs プロジェクト: omnimark/PTVS
 public PythonAst(Statement body, int[] lineLocations, PythonLanguageVersion langVersion) {
     if (body == null) {
         throw new ArgumentNullException("body");
     }
     _langVersion = langVersion;
     _body = body;
     _lineLocations = lineLocations;
 }
コード例 #5
0
ファイル: PythonKeywords.cs プロジェクト: omnimark/PTVS
 /// <summary>
 /// Returns true if the specified identifier is a statement keyword and
 /// never an expression in a particular version of Python.
 /// </summary>
 public static bool IsOnlyStatementKeyword(
     string keyword,
     PythonLanguageVersion version = PythonLanguageVersion.None
 ) {
     return Statement(version)
         .Except(Expression(version))
         .Contains(keyword, StringComparer.Ordinal);
 }
コード例 #6
0
        /// <summary>
        /// Create a RenameVariableRequestView with default values.
        /// </summary>
        public RenameVariableRequestView(string originalName, PythonLanguageVersion languageVersion) {
            _originalName = originalName;
            _languageVersion = languageVersion;
            //_name = null;

            // Access properties rather than underlying variables to ensure dependent properties
            // are also updated.
            Name = _originalName;
            _previewChanges = true;
        }
コード例 #7
0
 /// <summary>
 /// Create a RenameVariableRequestView with values taken from a template.
 /// </summary>
 public RenameVariableRequestView(
     string originalName,
     PythonLanguageVersion languageVersion,
     RenameVariableRequest template
 )
     : this(originalName, languageVersion) {
     // Access properties rather than underlying variables to ensure dependent properties
     // are also updated.
     Name = template.Name;
 }
コード例 #8
0
ファイル: MutateStdLibTest.cs プロジェクト: RussBaz/PTVS
        private static void TestOneFileMutated(string filename, PythonLanguageVersion version, Random random) {
            var originalText = File.ReadAllText(filename);
            int start = random.Next(originalText.Length);
            int end = random.Next(originalText.Length);

            int realStart = Math.Min(start, end);
            int length = Math.Max(start, end) - Math.Min(start, end);
            //Console.WriteLine("Removing {1} chars at {0}", realStart, length);
            originalText = originalText.Substring(realStart, length);

            ParserRoundTripTest.TestOneString(version, originalText);
        }
コード例 #9
0
ファイル: PythonClassifier.cs プロジェクト: wenh123/PTVS
        private static Dictionary<PythonLanguageVersion, Tokenizer> _tokenizers;    // tokenizer for each version, shared between all buffers

        internal PythonClassifier(PythonClassifierProvider provider, ITextBuffer buffer) {
            buffer.Changed += BufferChanged;
            buffer.ContentTypeChanged += BufferContentTypeChanged;

            _tokenCache = new TokenCache();
            _provider = provider;
            _buffer = buffer;

            var analyzer = _buffer.GetAnalyzer(provider._serviceProvider);
            Debug.Assert(analyzer != null);
            _version = analyzer.InterpreterFactory.GetLanguageVersion();
        }
コード例 #10
0
        public RenameVariableRequest GetRenameInfo(string originalName, PythonLanguageVersion languageVersion) {
            var requestView = new RenameVariableRequestView(originalName, languageVersion);
            LoadPreferences(requestView);
            var dialog = new RenameVariableDialog(requestView);
            bool res = dialog.ShowModal() ?? false;
            if (res) {
                SavePreferences(requestView);
                return requestView.GetRequest();
            }

            return null;
        }
コード例 #11
0
ファイル: AstPythonModule.cs プロジェクト: jsschultz/PTVS
        public static IPythonModule FromStream(
            IPythonInterpreter interpreter,
            Stream sourceFile,
            string fileName,
            PythonLanguageVersion langVersion
        ) {
            PythonAst ast;
            using (var parser = Parser.CreateParser(sourceFile, langVersion)) {
                ast = parser.ParseFile();
            }

            return new AstPythonModule(interpreter, ast, fileName);
        }
コード例 #12
0
ファイル: PythonClassifier.cs プロジェクト: wenh123/PTVS
        internal void NewVersion() {
            _tokenCache.Clear();

            var analyzer = _buffer.GetAnalyzer(_provider._serviceProvider);
            Debug.Assert(analyzer != null);
            _version = analyzer.InterpreterFactory.GetLanguageVersion();

            var changed = ClassificationChanged;
            if (changed != null) {
                var snapshot = _buffer.CurrentSnapshot;

                changed(this, new ClassificationChangedEventArgs(new SnapshotSpan(snapshot, 0, snapshot.Length)));
            }
        }
コード例 #13
0
ファイル: PythonClassifier.cs プロジェクト: RussBaz/PTVS
        private void NewAnalysisEntry(AnalysisEntry entry) {
            var analyzer = entry.Analyzer;
            var newVersion = _version;
            if (newVersion != _version) {
                _tokenCache.Clear();

                Debug.Assert(analyzer != null);
                _version = analyzer.InterpreterFactory.GetLanguageVersion();

                var changed = ClassificationChanged;
                if (changed != null) {
                    var snapshot = _buffer.CurrentSnapshot;

                    changed(this, new ClassificationChangedEventArgs(new SnapshotSpan(snapshot, 0, snapshot.Length)));
                }
            }
        }
コード例 #14
0
ファイル: PythonProcess.cs プロジェクト: KevinH-MS/PTVS
        private PythonProcess(int pid, PythonDebugOptions debugOptions) {
            _pid = pid;
            _process = Process.GetProcessById(pid);
            _process.EnableRaisingEvents = true;
            _process.Exited += new EventHandler(_process_Exited);

            ListenForConnection();

            using (var result = DebugAttach.AttachAD7(pid, DebugConnectionListener.ListenerPort, _processGuid, debugOptions.ToString())) {
                if (result.Error != ConnErrorMessages.None) {
                    throw new ConnectionException(result.Error);
                }

                _langVersion = (PythonLanguageVersion)result.LanguageVersion;
                if (!result.AttachDone.WaitOne(20000)) {
                    throw new ConnectionException(ConnErrorMessages.TimeOut);
                }
            }
        }
コード例 #15
0
ファイル: Parser.cs プロジェクト: omnimark/PTVS
        private Parser(Tokenizer tokenizer, ErrorSink errorSink, PythonLanguageVersion langVersion, bool verbatim, bool bindRefs, string privatePrefix) {
            Contract.Assert(tokenizer != null);
            Contract.Assert(errorSink != null);

            tokenizer.ErrorSink = new TokenizerErrorSink(this);

            _tokenizer = tokenizer;
            _errors = errorSink;
            _langVersion = langVersion;
            _verbatim = verbatim;
            _bindReferences = bindRefs;
            
            if (langVersion.Is3x()) {
                // 3.x always does true division and absolute import
                _languageFeatures |= FutureOptions.TrueDivision | FutureOptions.AbsoluteImports;
            }

            Reset(FutureOptions.None);

            _privatePrefix = privatePrefix;
        }
コード例 #16
0
 public PathResolverSnapshot(PythonLanguageVersion pythonLanguageVersion)
     : this(pythonLanguageVersion, string.Empty, Array.Empty <string>(), Array.Empty <string>(), ImmutableArray <Node> .Empty, 0, Node.CreateDefaultRoot(), Node.CreateBuiltinRoot(), default)
 {
 }
コード例 #17
0
ファイル: ExtractMethodTests.cs プロジェクト: RussBaz/PTVS
 private string[] GetAssignments(string testCase, PythonLanguageVersion version) {
     var ast = Parser.CreateParser(new StringReader(testCase), version).ParseFile();
     var walker = new TestAssignmentWalker();
     ast.Walk(walker);
     return walker._names.ToArray();
 }
コード例 #18
0
 public static IPythonModule FromFile(IPythonInterpreter interpreter, string sourceFile, PythonLanguageVersion langVersion)
 {
     using (var stream = new FileStream(sourceFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) {
         return(FromStream(interpreter, stream, sourceFile, langVersion));
     }
 }
コード例 #19
0
        private static void AssertHasExpression(string content, bool expectExpression, int index = -1, PythonLanguageVersion version = PythonLanguageVersion.V35)
        {
            var buffer = new MockTextBuffer(content);

            if (index < 0)
            {
                index += content.Length + 1;
            }
            var pt     = new SnapshotPoint(buffer.CurrentSnapshot, index);
            var actual = PythonTextBufferInfo.ForBuffer(null, buffer).IsPossibleExpressionAtPoint(pt);

            if (expectExpression)
            {
                Assert.IsTrue(actual, content);
            }
            else
            {
                Assert.IsFalse(actual, content);
            }
        }
コード例 #20
0
ファイル: PythonEditor.cs プロジェクト: JoshVarty/PTVS
        public PythonEditor(
            string content = null,
            PythonLanguageVersion version = PythonLanguageVersion.V27,
            MockVs vs = null,
            IPythonInterpreterFactory factory = null,
            VsProjectAnalyzer analyzer        = null,
            string filename = null
            )
        {
            if (vs == null)
            {
                _disposeVS = true;
                vs         = new MockVs();
            }
            MockVsTextView view = null;

            try {
                AdvancedEditorOptions advancedOptions = null;
                vs.InvokeSync(() => {
                    advancedOptions = vs.GetPyService().AdvancedOptions;
                    advancedOptions.AutoListMembers     = true;
                    advancedOptions.AutoListIdentifiers = false;
                });
                AdvancedOptions = advancedOptions;

                if (factory == null)
                {
                    _disposeFactory = true;
                    factory         = InterpreterFactoryCreator.CreateAnalysisInterpreterFactory(version.ToVersion());
                }
                if (analyzer == null)
                {
                    _disposeAnalyzer = true;
                    vs.InvokeSync(() => {
                        analyzer = new VsProjectAnalyzer(vs.ServiceProvider, factory);
                    });
                    var task = analyzer.ReloadTask;
                    if (task != null)
                    {
                        task.WaitAndUnwrapExceptions();
                    }
                }

                var cts = new CancellationTokenSource(TimeSpan.FromSeconds(60));
                using (var mre = new ManualResetEventSlim()) {
                    EventHandler evt = (s, e) => mre.SetIfNotDisposed();
                    analyzer.AnalysisStarted += evt;
                    view = vs.CreateTextView(PythonCoreConstants.ContentType, content ?? "", v => {
                        v.TextView.TextBuffer.Properties.AddProperty(typeof(VsProjectAnalyzer), analyzer);
                    }, filename);

                    try {
                        while (!mre.Wait(500, cts.Token) && !vs.HasPendingException)
                        {
                        }
                        analyzer.WaitForCompleteAnalysis(x => !cts.IsCancellationRequested && !vs.HasPendingException);
                    } catch (OperationCanceledException) {
                    } finally {
                        analyzer.AnalysisStarted -= evt;
                    }
                    if (cts.IsCancellationRequested)
                    {
                        Assert.Fail("Timed out waiting for code analysis");
                    }
                    vs.ThrowPendingException();
                }

                View     = view;
                view     = null;
                Analyzer = analyzer;
                analyzer = null;
                Factory  = factory;
                factory  = null;
                VS       = vs;
                vs       = null;
            } finally {
                if (view != null)
                {
                    view.Dispose();
                }
                if (analyzer != null && _disposeAnalyzer)
                {
                    analyzer.Dispose();
                }
                if (factory != null && _disposeFactory)
                {
                    var disp = factory as IDisposable;
                    if (disp != null)
                    {
                        disp.Dispose();
                    }
                }
                if (vs != null && _disposeVS)
                {
                    vs.Dispose();
                }
            }
        }
コード例 #21
0
 protected Task <IDocumentAnalysis> GetAnalysisAsync(string code, PythonLanguageVersion version, IServiceManager sm = null, string modulePath = null)
 => GetAnalysisAsync(code, PythonVersions.GetRequiredCPythonConfiguration(version), sm, modulePath);
コード例 #22
0
ファイル: IronPythonAnalysisTest.cs プロジェクト: yuts/PTVS
 protected override bool ShouldUseUnicodeLiterals(PythonLanguageVersion version)
 {
     return(true);
 }
コード例 #23
0
        private static ExpressionAnalysis AnalyzeExpression(MockVs vs, int location, string code, PythonLanguageVersion version = PythonLanguageVersion.V27)
        {
            if (location < 0)
            {
                location += code.Length + 1;
            }

            using (var view = new PythonEditor(code, version, vs)) {
                var snapshot = view.CurrentSnapshot;
                return(snapshot.AnalyzeExpression(
                           vs.ServiceProvider,
                           snapshot.CreateTrackingSpan(location, location < snapshot.Length ? 1 : 0, SpanTrackingMode.EdgeInclusive),
                           false
                           ));
            }
        }
コード例 #24
0
        private static void TestOneFile(string filename, PythonLanguageVersion version, TokenizerOptions optionSet)
        {
            var originalText = File.ReadAllText(filename);

            TestOneString(version, optionSet, originalText);
        }
コード例 #25
0
 public static Version ToVersion(this PythonLanguageVersion version)
 {
     return(new Version(((int)version) >> 8, ((int)version) & 0xff));
 }
コード例 #26
0
 public static bool IsNone(this PythonLanguageVersion version)
 {
     return(version == PythonLanguageVersion.None);
 }
コード例 #27
0
 public static bool Is3x(this PythonLanguageVersion version)
 {
     return((((int)version >> 8) & 0xff) == 3);
 }
コード例 #28
0
ファイル: AstPythonModule.cs プロジェクト: shiyongde/PTVS
 public static IPythonModule FromStream(
     IPythonInterpreter interpreter,
     Stream sourceFile,
     string fileName,
     PythonLanguageVersion langVersion
     ) => FromStream(interpreter, sourceFile, fileName, langVersion, null);
コード例 #29
0
ファイル: PythonPaths.cs プロジェクト: xoriath/PTVS
        private static PythonVersion GetCPythonVersion(PythonLanguageVersion version, bool x64 = false)
        {
            if (!x64)
            {
                foreach (var baseKey in new[] { Registry.LocalMachine, Registry.CurrentUser })
                {
                    using (var python = baseKey.OpenSubKey(PythonCorePath)) {
                        var res = TryGetCPythonPath(version, python, x64);
                        if (res != null)
                        {
                            return(res);
                        }
                    }
                }
            }

            if (Environment.Is64BitOperatingSystem && x64)
            {
                foreach (var baseHive in new[] { RegistryHive.LocalMachine, RegistryHive.CurrentUser })
                {
                    var python64 = RegistryKey.OpenBaseKey(baseHive, RegistryView.Registry64).OpenSubKey(PythonCorePath);
                    var res      = TryGetCPythonPath(version, python64, x64);
                    if (res != null)
                    {
                        return(res);
                    }
                }
            }

            var path = "C:\\Python" + version.ToString().Substring(1) + "\\python.exe";
            var arch = NativeMethods.GetBinaryType(path);

            if (arch == ProcessorArchitecture.X86 && !x64)
            {
                return(new PythonVersion(path, version,
                                         CPythonInterpreterFactoryConstants.GetInterpreterId(
                                             "PythonCore",
                                             arch,
                                             version.ToVersion().ToString()
                                             ),
                                         false,
                                         false,
                                         true
                                         ));
            }
            else if (arch == ProcessorArchitecture.Amd64 && x64)
            {
                return(new PythonVersion(
                           path,
                           version,
                           CPythonInterpreterFactoryConstants.GetInterpreterId(
                               "PythonCore",
                               arch,
                               version.ToVersion().ToString()
                               ),
                           true,
                           false,
                           true
                           ));
            }

            if (x64)
            {
                path = "C:\\Python" + version.ToString().Substring(1) + "_x64\\python.exe";
                arch = NativeMethods.GetBinaryType(path);
                if (arch == ProcessorArchitecture.Amd64)
                {
                    return(new PythonVersion(
                               path,
                               version,
                               CPythonInterpreterFactoryConstants.GetInterpreterId(
                                   "PythonCore",
                                   arch,
                                   version.ToVersion().ToString()
                                   ),
                               true,
                               false,
                               true

                               ));
                }
            }

            return(null);
        }
コード例 #30
0
 private PythonRemoteProcess(int pid, Uri uri, PythonLanguageVersion langVer, TextWriter debugLog)
     : base(pid, langVer, debugLog)
 {
     Uri = uri;
     ParseQueryString();
 }
コード例 #31
0
        public string GetConstantRepr(PythonLanguageVersion version, bool escape8bitStrings = false)
        {
            if (Value == null)
            {
                return("None");
            }
            else if (Value is AsciiString)
            {
                var res = new StringBuilder();
                if (!version.Is2x())
                {
                    res.Append("b");
                }
                AppendEscapedString(res, ((AsciiString)Value).String, escape8bitStrings);
                return(res.ToString());
            }
            else if (Value is string)
            {
                var res = new StringBuilder();
                if (!version.Is3x())
                {
                    res.Append("u");
                }
                AppendEscapedString(res, (string)Value, escape8bitStrings);
                return(res.ToString());
            }
            else if (Value is Complex)
            {
                var n    = (Complex)Value;
                var real = NegativeZeroAwareToString(n.Real);
                var imag = NegativeZeroAwareToString(n.Imaginary);
                if (n.Real != 0)
                {
                    if (!imag.StartsWithOrdinal("-"))
                    {
                        imag = "+" + imag;
                    }
                    return("(" + real + imag + "j)");
                }
                else
                {
                    return(imag + "j");
                }
            }
            else if (Value is BigInteger)
            {
                if (!version.Is3x())
                {
                    return("{0}L".FormatInvariant(Value));
                }
            }
            else if (Value is double n)
            {
                var s = NegativeZeroAwareToString(n);
                // If there's no fractional part, and this is not NaN or +-Inf, G format will not include the decimal
                // point. This is okay if we're using scientific notation as this implies float, but if not, add the
                // decimal point to indicate the type, just like Python repr() does.
                if ((n - Math.Truncate(n)) == 0 && !s.Contains("e"))
                {
                    s += ".0";
                }
                return(s);
            }
            else if (Value is IFormattable)
            {
                return(((IFormattable)Value).ToString(null, CultureInfo.InvariantCulture));
            }

            // TODO: We probably need to handle more primitives here
            return(Value.ToString());
        }
コード例 #32
0
 private static IEnumerable <string> GetCompletions(MockVs vs, int index, string code, PythonLanguageVersion version = PythonLanguageVersion.V27)
 {
     using (var view = new PythonEditor(code, version, vs)) {
         return(view.GetCompletions(index));
     }
 }
コード例 #33
0
        private static async Task PasteReplCode(
            IInteractiveWindow window,
            string pasting,
            PythonLanguageVersion version
            )
        {
            // there's some text in the buffer...
            var view  = window.TextView;
            var caret = view.Caret;

            if (view.Selection.IsActive && !view.Selection.IsEmpty)
            {
                foreach (var span in view.Selection.SelectedSpans)
                {
                    foreach (var normalizedSpan in view.BufferGraph.MapDownToBuffer(span, SpanTrackingMode.EdgeInclusive, window.CurrentLanguageBuffer))
                    {
                        normalizedSpan.Snapshot.TextBuffer.Delete(normalizedSpan);
                    }
                }
            }

            var curBuffer  = window.CurrentLanguageBuffer;
            var inputPoint = view.BufferGraph.MapDownToBuffer(
                caret.Position.BufferPosition,
                PointTrackingMode.Positive,
                curBuffer,
                PositionAffinity.Successor
                );


            // if we didn't find a location then see if we're in a prompt, and if so, then we want
            // to insert after the prompt.
            if (caret.Position.BufferPosition != window.TextView.TextBuffer.CurrentSnapshot.Length)
            {
                for (int i = caret.Position.BufferPosition + 1;
                     inputPoint == null && i <= window.TextView.TextBuffer.CurrentSnapshot.Length;
                     i++)
                {
                    inputPoint = view.BufferGraph.MapDownToBuffer(
                        new SnapshotPoint(window.TextView.TextBuffer.CurrentSnapshot, i),
                        PointTrackingMode.Positive,
                        curBuffer,
                        PositionAffinity.Successor
                        );
                }
            }

            if (inputPoint == null)
            {
                // we didn't find a point to insert, insert at the beginning.
                inputPoint = new SnapshotPoint(curBuffer.CurrentSnapshot, 0);
            }

            // we want to insert the pasted code at the caret, but we also want to
            // respect the stepping.  So first grab the code before and after the caret.
            string startText = curBuffer.CurrentSnapshot.GetText(0, inputPoint.Value);

            string endText = curBuffer.CurrentSnapshot.GetText(
                inputPoint.Value,
                curBuffer.CurrentSnapshot.Length - inputPoint.Value);


            var splitCode = JoinToCompleteStatements(SplitAndDedent(startText + pasting + endText), version).ToList();

            curBuffer.Delete(new Span(0, curBuffer.CurrentSnapshot.Length));

            bool supportMultiple = window.GetPythonEvaluator()?.SupportsMultipleStatements ?? false;

            if (supportMultiple)
            {
                await window.SubmitAsync(new[] { string.Join(Environment.NewLine, splitCode) });
            }
            else if (splitCode.Count == 1)
            {
                curBuffer.Insert(0, splitCode[0]);
                var viewPoint = view.BufferGraph.MapUpToBuffer(
                    new SnapshotPoint(curBuffer.CurrentSnapshot, Math.Min(inputPoint.Value.Position + pasting.Length, curBuffer.CurrentSnapshot.Length)),
                    PointTrackingMode.Positive,
                    PositionAffinity.Successor,
                    view.TextBuffer
                    );

                if (viewPoint != null)
                {
                    view.Caret.MoveTo(viewPoint.Value);
                }
            }
            else if (splitCode.Count != 0)
            {
                var lastCode = splitCode[splitCode.Count - 1];
                splitCode.RemoveAt(splitCode.Count - 1);

                while (splitCode.Any())
                {
                    var code = splitCode[0];
                    splitCode.RemoveAt(0);
                    await window.SubmitAsync(new[] { code });

                    supportMultiple = window.GetPythonEvaluator()?.SupportsMultipleStatements ?? false;
                    if (supportMultiple)
                    {
                        // Might have changed while we were executing
                        break;
                    }
                }

                if (supportMultiple)
                {
                    // Submit all remaning lines of code
                    await window.SubmitAsync(new[] { string.Join(Environment.NewLine, splitCode) });
                }
                else
                {
                    window.CurrentLanguageBuffer.Insert(0, lastCode);
                }
            }
            else
            {
                window.CurrentLanguageBuffer.Insert(0, startText + pasting + endText);
            }
        }
コード例 #34
0
ファイル: ParserRoundTripTest.cs プロジェクト: sadapple/PTVS
        private static void TestOneFile(string filename, PythonLanguageVersion version) {
            var originalText = File.ReadAllText(filename);

            TestOneString(version, originalText);
        }
コード例 #35
0
        internal PythonAnalyzer AnalyzeDir(string dir, PythonLanguageVersion version = PythonLanguageVersion.V27, IEnumerable <string> excludeDirectories = null, CancellationToken?cancel = null)
        {
            List <string> files = new List <string>();

            try {
                ISet <string> excluded = null;
                if (excludeDirectories != null)
                {
                    excluded = new HashSet <string>(excludeDirectories, StringComparer.InvariantCultureIgnoreCase);
                }
                CollectFiles(dir, files, excluded);
            } catch (DirectoryNotFoundException) {
                return(null);
            }

            List <FileStreamReader> sourceUnits = new List <FileStreamReader>();

            foreach (string file in files)
            {
                sourceUnits.Add(
                    new FileStreamReader(file)
                    );
            }

            Stopwatch sw = new Stopwatch();

            sw.Start();
            long start0 = sw.ElapsedMilliseconds;

            var fact         = InterpreterFactoryCreator.CreateAnalysisInterpreterFactory(version.ToVersion());
            var projectState = new PythonAnalyzer(fact, fact.CreateInterpreter());

            projectState.ReloadModulesAsync(cancel ?? CancellationToken.None).WaitAndUnwrapExceptions();

            projectState.Limits = AnalysisLimits.GetStandardLibraryLimits();

            var modules = new List <IPythonProjectEntry>();

            foreach (var sourceUnit in sourceUnits)
            {
                try {
                    modules.Add(projectState.AddModule(
                                    ModulePath.FromFullPath(sourceUnit.Path).ModuleName,
                                    sourceUnit.Path,
                                    null
                                    ));
                } catch (ArgumentException) {
                    // Invalid module name, so skip the module
                }
            }
            long start1 = sw.ElapsedMilliseconds;

            Trace.TraceInformation("AddSourceUnit: {0} ms", start1 - start0);

            var nodes = new List <Microsoft.PythonTools.Parsing.Ast.PythonAst>();

            for (int i = 0; i < modules.Count; i++)
            {
                PythonAst ast = null;
                try {
                    var sourceUnit = sourceUnits[i];

                    ast = Parser.CreateParser(sourceUnit, version).ParseFile();
                } catch (Exception) {
                }
                nodes.Add(ast);
            }
            long start2 = sw.ElapsedMilliseconds;

            Trace.TraceInformation("Parse: {0} ms", start2 - start1);

            for (int i = 0; i < modules.Count; i++)
            {
                var ast = nodes[i];

                if (ast != null)
                {
                    using (var p = modules[i].BeginParse()) {
                        p.Tree = ast;
                        p.Complete();
                    }
                }
            }

            long start3 = sw.ElapsedMilliseconds;

            for (int i = 0; i < modules.Count; i++)
            {
                Trace.TraceInformation("Analyzing {1}: {0} ms", sw.ElapsedMilliseconds - start3, sourceUnits[i].Path);
                var ast = nodes[i];
                if (ast != null)
                {
                    modules[i].Analyze(cancel ?? CancellationToken.None, true);
                }
            }
            if (modules.Count > 0)
            {
                Trace.TraceInformation("Analyzing queue");
                modules[0].AnalysisGroup.AnalyzeQueuedEntries(cancel ?? CancellationToken.None);
            }

            long start4 = sw.ElapsedMilliseconds;

            Trace.TraceInformation("Analyze: {0} ms", start4 - start3);
            return(projectState);
        }
コード例 #36
0
        public async Task RenameVariable(IRenameVariableInput input, IVsPreviewChangesService previewChanges)
        {
            if (IsModuleName(input))
            {
                input.CannotRename(Strings.RenameVariable_CannotRenameModuleName);
                return;
            }

            var caret = _view.GetPythonCaret();
            var entry = _view.GetAnalysisAtCaret(_serviceProvider);

            if (caret == null || entry == null)
            {
                input.CannotRename(Strings.RenameVariable_UnableGetAnalysisCurrentTextView);
                return;
            }
            var analysis = await entry.Analyzer.AnalyzeExpressionAsync(entry, caret.Value);

            if (analysis == null)
            {
                input.CannotRename(Strings.RenameVariable_UnableGetAnalysisCurrentTextView);
                return;
            }

            string originalName  = null;
            string privatePrefix = null;

            if (!String.IsNullOrWhiteSpace(analysis.Expression))
            {
                originalName = analysis.MemberName;

                if (analysis.PrivatePrefix != null && originalName != null && originalName.StartsWith("_" + analysis.PrivatePrefix))
                {
                    originalName  = originalName.Substring(analysis.PrivatePrefix.Length + 1);
                    privatePrefix = analysis.PrivatePrefix;
                }

                if (originalName != null && _view.Selection.IsActive && !_view.Selection.IsEmpty)
                {
                    if (_view.Selection.Start.Position < analysis.Span.GetStartPoint(_view.TextBuffer.CurrentSnapshot) ||
                        _view.Selection.End.Position > analysis.Span.GetEndPoint(_view.TextBuffer.CurrentSnapshot))
                    {
                        originalName = null;
                    }
                }
            }

            if (originalName == null)
            {
                input.CannotRename(Strings.RenameVariable_SelectSymbol);
                return;
            }

            bool hasVariables = false;

            foreach (var variable in analysis.Variables)
            {
                if (variable.Type == VariableType.Definition || variable.Type == VariableType.Reference)
                {
                    hasVariables = true;
                    break;
                }
            }

            IEnumerable <AnalysisVariable> variables;

            if (!hasVariables)
            {
                List <AnalysisVariable> paramVars = await GetKeywordParameters(analysis.Expression, originalName);

                if (paramVars.Count == 0)
                {
                    input.CannotRename(Strings.RenameVariable_NoInformationAvailableForVariable.FormatUI(originalName));
                    return;
                }

                variables = paramVars;
            }
            else
            {
                variables = analysis.Variables;
            }

            PythonLanguageVersion languageVersion = PythonLanguageVersion.None;
            var analyzer = _view.GetAnalyzerAtCaret(_serviceProvider);
            var factory  = analyzer != null ? analyzer.InterpreterFactory : null;

            if (factory != null)
            {
                languageVersion = factory.Configuration.Version.ToLanguageVersion();
            }

            var info = input.GetRenameInfo(originalName, languageVersion);

            if (info != null)
            {
                var engine = new PreviewChangesEngine(_serviceProvider, input, analysis.Expression, info, originalName, privatePrefix, _view.GetAnalyzerAtCaret(_serviceProvider), variables);
                if (info.Preview)
                {
                    previewChanges.PreviewChanges(engine);
                }
                else
                {
                    ErrorHandler.ThrowOnFailure(engine.ApplyChanges());
                }
            }
        }
コード例 #37
0
        private static void AssertExpression(string content, string expected, int index = -1, PythonLanguageVersion version = PythonLanguageVersion.V35)
        {
            var buffer = new MockTextBuffer(content);

            if (index < 0)
            {
                index += content.Length + 1;
            }
            var span   = new SnapshotSpan(buffer.CurrentSnapshot, index, 0);
            var actual = PythonTextBufferInfo.ForBuffer(null, buffer).GetExpressionAtPoint(span, GetExpressionOptions.Hover);

            Assert.AreEqual(expected, actual?.GetText(), content);
        }
コード例 #38
0
        /// <summary>
        /// Gets the current AST and the code string for the project entry and returns the current code.
        /// </summary>
        public static PythonAst GetVerbatimAstAndCode(this IPythonProjectEntry projectFile, PythonLanguageVersion langVersion, int bufferId, out int version, out string code) {
            ParserOptions options = new ParserOptions { BindReferences = true, Verbatim = true };

            code = projectFile.GetCurrentCode(bufferId, out version);
            if (code != null) {
                var parser = Parser.CreateParser(
                    new StringReader(code),
                    langVersion,
                    options
                );

                return parser.ParseFile();
            }
            return null;
        }
コード例 #39
0
ファイル: PythonRemoteProcess.cs プロジェクト: omnimark/PTVS
 private PythonRemoteProcess(int pid, Uri uri, PythonLanguageVersion langVer)
     : base(pid, langVer) {
     Uri = uri;
     ParseQueryString();
 }
コード例 #40
0
        internal static IEnumerable <string> JoinToCompleteStatements(IEnumerable <string> lines, PythonLanguageVersion version, bool fixNewLine = true)
        {
            StringBuilder temp            = new StringBuilder();
            string        prevText        = null;
            ParseResult?  prevParseResult = null;

            using (var e = new PeekableEnumerator <string>(lines)) {
                bool skipNextMoveNext = false;
                while (skipNextMoveNext || e.MoveNext())
                {
                    skipNextMoveNext = false;
                    var line = e.Current;

                    if (e.HasNext)
                    {
                        temp.AppendLine(line);
                    }
                    else
                    {
                        temp.Append(line);
                    }
                    string newCode = temp.ToString();

                    var         parser = Parser.CreateParser(new StringReader(newCode), version);
                    ParseResult result;
                    parser.ParseInteractiveCode(out result);

                    // if this parse is invalid then we need more text to be valid.
                    // But if this text is invalid and the previous parse was incomplete
                    // then appending more text won't fix things - the code in invalid, the user
                    // needs to fix it, so let's not break it up which would prevent that from happening.
                    if (result == ParseResult.Empty)
                    {
                        if (!String.IsNullOrWhiteSpace(newCode))
                        {
                            // comment line, include w/ following code.
                            prevText        = newCode;
                            prevParseResult = result;
                        }
                        else
                        {
                            temp.Clear();
                        }
                    }
                    else if (result == ParseResult.Complete)
                    {
                        yield return(FixEndingNewLine(newCode, fixNewLine));

                        temp.Clear();

                        prevParseResult = null;
                        prevText        = null;
                    }
                    else if (ShouldAppendCode(prevParseResult, result))
                    {
                        prevText        = newCode;
                        prevParseResult = result;
                    }
                    else if (prevText != null)
                    {
                        // we have a complete input
                        yield return(FixEndingNewLine(prevText, fixNewLine));

                        temp.Clear();

                        // reparse this line so our state remains consistent as if we just started out.
                        skipNextMoveNext = true;
                        prevParseResult  = null;
                        prevText         = null;
                    }
                    else
                    {
                        prevParseResult = result;
                    }
                }
            }

            if (temp.Length > 0)
            {
                yield return(FixEndingNewLine(temp.ToString(), fixNewLine));
            }
        }
コード例 #41
0
ファイル: Parser.cs プロジェクト: omnimark/PTVS
        public static Parser CreateParser(TextReader reader, PythonLanguageVersion version, ParserOptions parserOptions) {
            if (reader == null) {
                throw new ArgumentNullException("reader");
            }

            var options = parserOptions ?? ParserOptions.Default;

            Parser parser = null;
            var tokenizer = new Tokenizer(
                version, options.ErrorSink,
                (options.Verbatim ? TokenizerOptions.Verbatim : TokenizerOptions.None) | TokenizerOptions.GroupingRecovery,
                (span, text) => options.RaiseProcessComment(parser, new CommentEventArgs(span, text)));
            tokenizer.Initialize(null, reader, SourceLocation.MinValue);
            tokenizer.IndentationInconsistencySeverity = options.IndentationInconsistencySeverity;

            parser = new Parser(
                tokenizer,
                options.ErrorSink ?? ErrorSink.Null,
                version,
                options.Verbatim,
                options.BindReferences,
                options.PrivatePrefix
            ) { _sourceReader = reader };
            return parser;
        }
コード例 #42
0
ファイル: PythonProcess.cs プロジェクト: KevinH-MS/PTVS
 public static PythonProcess AttachRepl(Stream stream, int pid, PythonLanguageVersion version, PythonDebugOptions debugOptions = PythonDebugOptions.None) {
     return new PythonProcess(stream, pid, version, debugOptions);
 }
コード例 #43
0
        private IEnumerable <Task> StartCrossThreadAnalysisCalls(
            CancellationToken cancel,
            PythonLanguageVersion version
            )
        {
            var fact  = InterpreterFactoryCreator.CreateAnalysisInterpreterFactory(version.ToVersion());
            var state = PythonAnalyzer.CreateSynchronously(fact);

            const string testCode = @"from mod{0:000} import test_func as other_test_func, MyClass as other_mc

c = None
def test_func(a, b={1}()):
    '''My test function'''
    globals c
    a = b
    a = {1}(a)
    b = other_test_func(a)
    c = other_mc.fn(b)
    return b

class MyClass:
    fn = test_func

my_test_func = test_func
my_test_func = other_test_func
my_test_func('abc')

mc = MyClass()
mc.fn([])
";

            var entries = Enumerable.Range(0, 100)
                          .Select(i => {
                var entry = state.AddModule(string.Format("mod{0:000}", i), string.Format("mod{0:000}.py", i));
                entry.ParseFormat(PythonLanguageVersion.V34, testCode, i + 1, PythonTypes[i % PythonTypes.Count]);
                return(entry);
            })
                          .ToList();

            // One analysis before we start
            foreach (var e in entries)
            {
                e.Analyze(cancel, true);
            }
            state.AnalyzeQueuedEntries(cancel);

            // Repeatedly re-analyse the code
            yield return(Task.Run(() => {
                var rnd = new Random();
                while (!cancel.IsCancellationRequested)
                {
                    var shufEntries = entries
                                      .Select(e => Tuple.Create(rnd.Next(), e))
                                      .OrderBy(t => t.Item1)
                                      .Take(entries.Count / 2)
                                      .Select(t => t.Item2)
                                      .ToList();
                    foreach (var e in shufEntries)
                    {
                        e.Analyze(cancel, true);
                    }

                    state.AnalyzeQueuedEntries(cancel);
                    Console.WriteLine("Analysis complete");
                    Thread.Sleep(1000);
                }
            }, cancel));

            // Repeatedly re-parse the code
            yield return(Task.Run(() => {
                var rnd = new Random();
                while (!cancel.IsCancellationRequested)
                {
                    var shufEntries = entries
                                      .Select((e, i) => Tuple.Create(rnd.Next(), e, i))
                                      .OrderBy(t => t.Item1)
                                      .Take(entries.Count / 4)
                                      .ToList();
                    foreach (var t in shufEntries)
                    {
                        var i = t.Item3;
                        t.Item2.ParseFormat(PythonLanguageVersion.V34, testCode, i + 1, PythonTypes[i % PythonTypes.Count]);
                    }
                    Thread.Sleep(1000);
                }
            }, cancel));

            // Repeatedly request signatures
            yield return(Task.Run(() => {
                var entry = entries[1];
                while (!cancel.IsCancellationRequested)
                {
                    var sigs = entry.Analysis.GetSignaturesByIndex("my_test_func", 0).ToList();

                    if (sigs.Any())
                    {
                        AssertUtil.ContainsExactly(
                            sigs.Select(s => s.Name),
                            "test_func"
                            );

                        foreach (var s in sigs)
                        {
                            AssertUtil.ContainsExactly(s.Parameters.Select(p => p.Name), "a", "b");
                        }
                    }
                }
            }, cancel));

            // Repeated request variables and descriptions
            yield return(Task.Run(() => {
                var entry = entries[1];
                while (!cancel.IsCancellationRequested)
                {
                    var descriptions = entry.Analysis.GetDescriptionsByIndex("my_test_func", 0).ToList();
                    descriptions = entry.Analysis.GetDescriptionsByIndex("c", 0).ToList();
                }
            }, cancel));

            // Repeated request members and documentation
            yield return(Task.Run(() => {
                var entry = entries[1];
                while (!cancel.IsCancellationRequested)
                {
                    var descriptions = entry.Analysis.GetCompletionDocumentationByIndex("mc", "fn", 0).ToList();
                }
            }, cancel));
        }
コード例 #44
0
ファイル: ThreadingTest.cs プロジェクト: RussBaz/PTVS
        private IEnumerable<Task> StartCrossThreadAnalysisCalls(
            CancellationToken cancel,
            PythonLanguageVersion version
        ) {
            var fact = InterpreterFactoryCreator.CreateAnalysisInterpreterFactory(version.ToVersion());
            var state = PythonAnalyzer.CreateSynchronously(fact);

            const string testCode = @"from mod{0:000} import test_func as other_test_func, MyClass as other_mc

c = None
def test_func(a, b={1}()):
    '''My test function'''
    globals c
    a = b
    a = {1}(a)
    b = other_test_func(a)
    c = other_mc.fn(b)
    return b

class MyClass:
    fn = test_func

my_test_func = test_func
my_test_func = other_test_func
my_test_func('abc')

mc = MyClass()
mc.fn([])
";

            var entries = Enumerable.Range(0, 100)
                .Select(i => {
                    var entry = state.AddModule(string.Format("mod{0:000}", i), string.Format("mod{0:000}.py", i));
                    entry.ParseFormat(PythonLanguageVersion.V34, testCode, i + 1, PythonTypes[i % PythonTypes.Count]);
                    return entry;
                })
                .ToList();

            // One analysis before we start
            foreach (var e in entries) {
                e.Analyze(cancel, true);
            }
            state.AnalyzeQueuedEntries(cancel);

            // Repeatedly re-analyse the code
            yield return Task.Run(() => {
                var rnd = new Random();
                while (!cancel.IsCancellationRequested) {
                    var shufEntries = entries
                        .Select(e => Tuple.Create(rnd.Next(), e))
                        .OrderBy(t => t.Item1)
                        .Take(entries.Count / 2)
                        .Select(t => t.Item2)
                        .ToList();
                    foreach (var e in shufEntries) {
                        e.Analyze(cancel, true);
                    }

                    state.AnalyzeQueuedEntries(cancel);
                    Console.WriteLine("Analysis complete");
                    Thread.Sleep(1000);
                }
            }, cancel);

            // Repeatedly re-parse the code
            yield return Task.Run(() => {
                var rnd = new Random();
                while (!cancel.IsCancellationRequested) {
                    var shufEntries = entries
                        .Select((e, i) => Tuple.Create(rnd.Next(), e, i))
                        .OrderBy(t => t.Item1)
                        .Take(entries.Count / 4)
                        .ToList();
                    foreach (var t in shufEntries) {
                        var i = t.Item3;
                        t.Item2.ParseFormat(PythonLanguageVersion.V34, testCode, i + 1, PythonTypes[i % PythonTypes.Count]);
                    }
                    Thread.Sleep(1000);
                }
            }, cancel);

            // Repeatedly request signatures
            yield return Task.Run(() => {
                var entry = entries[1];
                while (!cancel.IsCancellationRequested) {
                    var sigs = entry.Analysis.GetSignaturesByIndex("my_test_func", 0).ToList();

                    if (sigs.Any()) {
                        AssertUtil.ContainsExactly(
                            sigs.Select(s => s.Name),
                            "test_func"
                        );

                        foreach (var s in sigs) {
                            AssertUtil.ContainsExactly(s.Parameters.Select(p => p.Name), "a", "b");
                        }
                    }
                }
            }, cancel);

            // Repeated request variables and descriptions
            yield return Task.Run(() => {
                var entry = entries[1];
                while (!cancel.IsCancellationRequested) {
                    var descriptions = entry.Analysis.GetDescriptionsByIndex("my_test_func", 0).ToList();
                    descriptions = entry.Analysis.GetDescriptionsByIndex("c", 0).ToList();
                }
            }, cancel);

            // Repeated request members and documentation
            yield return Task.Run(() => {
                var entry = entries[1];
                while (!cancel.IsCancellationRequested) {
                    var descriptions = entry.Analysis.GetCompletionDocumentationByIndex("mc", "fn", 0).ToList();
                }
            }, cancel);
        }
コード例 #45
0
ファイル: AstAnalysisTests.cs プロジェクト: jsschultz/PTVS
 private static IPythonModule Parse(string path, PythonLanguageVersion version) {
     var interpreter = InterpreterFactoryCreator.CreateAnalysisInterpreterFactory(version.ToVersion()).CreateInterpreter();
     if (!Path.IsPathRooted(path)) {
         path = TestData.GetPath(Path.Combine("TestData", "AstAnalysis", path));
     }
     return AstPythonModule.FromFile(interpreter, path, version);
 }
コード例 #46
0
ファイル: PythonTestExtensions.cs プロジェクト: omnimark/PTVS
 public static void Parse(this IPythonProjectEntry entry, PythonLanguageVersion version, string code) {
     using (var parser = Parser.CreateParser(new StringReader(code), version)) {
         entry.UpdateTree(parser.ParseFile(), null);
     }
 }
コード例 #47
0
        internal static Dictionary <CoverageFileInfo, CoverageMapper> Import(CoverageFileInfo[] fileInfo, PythonLanguageVersion version = PythonLanguageVersion.V27)
        {
            Dictionary <CoverageFileInfo, CoverageMapper> files = new Dictionary <CoverageFileInfo, CoverageMapper>();

            foreach (var file in fileInfo)
            {
                using (var stream = new FileStream(file.Filename, FileMode.Open)) {
                    var parser = Parser.CreateParser(stream, version);
                    var ast    = parser.ParseFile();

                    var collector = new CoverageMapper(ast, file.Filename, file.Hits);
                    ast.Walk(collector);

                    files[file] = collector;
                }
            }
            return(files);
        }
コード例 #48
0
ファイル: PythonDebugger.cs プロジェクト: xNUTs/PTVS
 /// <summary>
 /// Creates a new PythonProcess object for debugging.  The process does not start until Start is called
 /// on the returned PythonProcess object.
 /// </summary>
 public PythonProcess CreateProcess(PythonLanguageVersion langVersion, string exe, string args, string dir, string env, string interpreterOptions = null, PythonDebugOptions debugOptions = PythonDebugOptions.None)
 {
     return(new PythonProcess(langVersion, exe, args, dir, env, interpreterOptions, debugOptions));
 }
コード例 #49
0
ファイル: ParserRoundTripTest.cs プロジェクト: sadapple/PTVS
        internal static void TestOneString(PythonLanguageVersion version, string originalText, CodeFormattingOptions format = null, string expected = null, bool recurse = true) {
            bool hadExpected = true;
            if (expected == null) {
                expected = originalText;
                hadExpected = false;
            }
            var parser = Parser.CreateParser(new StringReader(originalText), version, new ParserOptions() { Verbatim = true });
            var ast = parser.ParseFile();

            string output;
            try {
                if (format == null) {
                    output = ast.ToCodeString(ast);
                } else {
                    output = ast.ToCodeString(ast, format);
                }
            } catch(Exception e) {
                Console.WriteLine("Failed to convert to code: {0}\r\n{1}", originalText, e);
                Assert.Fail();
                return;
            }

            const int contextSize = 50;
            for (int i = 0; i < expected.Length && i < output.Length; i++) {
                if (expected[i] != output[i]) {
                    // output some context
                    StringBuilder x = new StringBuilder();
                    StringBuilder y = new StringBuilder();
                    StringBuilder z = new StringBuilder();
                    for (int j = Math.Max(0, i - contextSize); j < Math.Min(Math.Max(expected.Length, output.Length), i + contextSize); j++) {
                        if (j < expected.Length) {
                            x.AppendRepr(expected[j]);
                        }
                        if (j < output.Length) {
                            y.AppendRepr(output[j]);
                        }
                        if (j == i) {
                            z.Append("^");
                        } else {
                            z.Append(" ");
                        }
                    }

                    Console.WriteLine("Mismatch context at {0}:", i);
                    Console.WriteLine("Expected: {0}", x.ToString());
                    Console.WriteLine("Got     : {0}", y.ToString());
                    Console.WriteLine("Differs : {0}", z.ToString());

                    if (recurse) {
                        // Try and automatically get a minimal repro if we can...
                        if (!hadExpected) {
                            try {
                                for (int j = i; j >= 0; j--) {
                                    TestOneString(version, originalText.Substring(j), format, null, false);
                                }
                            } catch {
                            }
                        }
                    } else {
                        Console.WriteLine("-----");
                        Console.WriteLine(expected);
                        Console.WriteLine("-----");
                    }

                    Assert.AreEqual(expected[i], output[i], String.Format("Characters differ at {0}, got {1}, expected {2}", i, output[i], expected[i]));
                }
            }

            if (expected.Length != output.Length) {
                Console.WriteLine("Original: {0}", expected.ToString());
                Console.WriteLine("New     : {0}", output.ToString());
            }
            Assert.AreEqual(expected.Length, output.Length);
        }        
コード例 #50
0
 public FallbackBuiltinModule(PythonLanguageVersion version)
     : base(BuiltinTypeId.Unknown.GetModuleName(version))
 {
     LanguageVersion  = version;
     _cachedInstances = new Dictionary <BuiltinTypeId, IMember>();
 }
コード例 #51
0
ファイル: PythonProcess.cs プロジェクト: KevinH-MS/PTVS
        public PythonProcess(PythonLanguageVersion languageVersion, string exe, string args, string dir, string env, string interpreterOptions, PythonDebugOptions options = PythonDebugOptions.None, List<string[]> dirMapping = null)
            : this(0, languageVersion) {

            ListenForConnection();

            if (dir.EndsWith("\\")) {
                dir = dir.Substring(0, dir.Length - 1);
            }
            _dirMapping = dirMapping;
            var processInfo = new ProcessStartInfo(exe);

            processInfo.CreateNoWindow = (options & PythonDebugOptions.CreateNoWindow) != 0;
            processInfo.UseShellExecute = false;
            processInfo.RedirectStandardOutput = false;
            processInfo.RedirectStandardInput = (options & PythonDebugOptions.RedirectInput) != 0;

            processInfo.Arguments = 
                (String.IsNullOrWhiteSpace(interpreterOptions) ? "" : (interpreterOptions + " ")) +
                "\"" + PythonToolsInstallPath.GetFile("visualstudio_py_launcher.py") + "\" " +
                "\"" + dir + "\" " +
                " " + DebugConnectionListener.ListenerPort + " " +
                " " + _processGuid + " " +
                "\"" + options + "\" " +
                args;

            if (env != null) {
                string[] envValues = env.Split('\0');
                foreach (var curValue in envValues) {
                    string[] nameValue = curValue.Split(new[] { '=' }, 2);
                    if (nameValue.Length == 2 && !String.IsNullOrWhiteSpace(nameValue[0])) {
                        processInfo.EnvironmentVariables[nameValue[0]] = nameValue[1];
                    }
                }
            }

            Debug.WriteLine(String.Format("Launching: {0} {1}", processInfo.FileName, processInfo.Arguments));
            _process = new Process();
            _process.StartInfo = processInfo;
            _process.EnableRaisingEvents = true;
            _process.Exited += new EventHandler(_process_Exited);
        }
コード例 #52
0
 public PythonAnalysis(PythonLanguageVersion version)
     : this(InterpreterFactoryCreator.CreateAnalysisInterpreterFactory(version.ToVersion()))
 {
 }
コード例 #53
0
ファイル: PythonProcess.cs プロジェクト: KevinH-MS/PTVS
 protected PythonProcess(int pid, PythonLanguageVersion languageVersion) {
     _pid = pid;
     _langVersion = languageVersion;
     _dirMapping = new List<string[]>();
 }
コード例 #54
0
 private PythonAst GetParse(string code, PythonLanguageVersion version)
 => Parser.CreateParser(new StringReader(code), version).ParseFile();
コード例 #55
0
ファイル: PythonProcess.cs プロジェクト: KevinH-MS/PTVS
        private PythonProcess(Stream stream, int pid, PythonLanguageVersion version, PythonDebugOptions debugOptions) {
            _pid = pid;
            _process = Process.GetProcessById(pid);
            _process.EnableRaisingEvents = true;
            _process.Exited += new EventHandler(_process_Exited);
            
            _delayUnregister = true;
            
            ListenForConnection();

            stream.WriteInt32(DebugConnectionListener.ListenerPort);
            stream.WriteString(_processGuid.ToString());
            stream.WriteString(debugOptions.ToString());
        }
コード例 #56
0
 public static IPythonModule FromFile(
     IPythonInterpreter interpreter,
     string sourceFile,
     PythonLanguageVersion langVersion
     ) => FromFile(interpreter, sourceFile, langVersion, null);
コード例 #57
0
        // TODO: turn PythonDebugOptions into a class that encapsulates all options (not just flags), including the "not set"
        // state for all of them, and that knows how to stringify and parse itself, and how to merge isntances, and refactor
        // this entire codepath, including the bits in DefaultPythonLauncher and in CustomDebuggerEventHandler, to use that.
        private void ParseOptions(string options)
        {
            foreach (var optionSetting in SplitOptions(options)) {
                var setting = optionSetting.Split(new[] { '=' }, 2);
                if (setting.Length == 2) {
                    switch (setting[0]) {
                        case VersionSetting:
                            _languageVersion = GetLanguageVersion(setting[1]);
                            break;

                        case WaitOnAbnormalExitSetting:
                            bool value;
                            if (Boolean.TryParse(setting[1], out value) && value) {
                                _debugOptions |= PythonDebugOptions.WaitOnAbnormalExit;
                            }
                            break;

                        case WaitOnNormalExitSetting:
                            if (Boolean.TryParse(setting[1], out value) && value) {
                                _debugOptions |= PythonDebugOptions.WaitOnNormalExit;
                            }
                            break;

                        case RedirectOutputSetting:
                            if (Boolean.TryParse(setting[1], out value) && value) {
                                _debugOptions |= PythonDebugOptions.RedirectOutput;
                            }
                            break;

                        case BreakSystemExitZero:
                            if (Boolean.TryParse(setting[1], out value) && value) {
                                _debugOptions |= PythonDebugOptions.BreakOnSystemExitZero;
                            }
                            break;

                        case DebugStdLib:
                            if (Boolean.TryParse(setting[1], out value) && value) {
                                _debugOptions |= PythonDebugOptions.DebugStdLib;
                            }
                            break;

                        case IsWindowsApplication:
                            if (Boolean.TryParse(setting[1], out value) && value) {
                                _debugOptions |= PythonDebugOptions.IsWindowsApplication;
                            }
                            break;

                        case DirMappingSetting:
                            string[] dirs = setting[1].Split('|');
                            if (dirs.Length == 2) {
                                if (_dirMapping == null) {
                                    _dirMapping = new List<string[]>();
                                }
                                Debug.WriteLine(String.Format("Mapping dir {0} to {1}", dirs[0], dirs[1]));
                                _dirMapping.Add(dirs);
                            }
                            break;

                        case InterpreterOptions:
                            _interpreterOptions = setting[1];
                            break;

                        case AttachRunning:
                            if (Boolean.TryParse(setting[1], out value) && value) {
                                _debugOptions |= PythonDebugOptions.AttachRunning;
                            }
                            break;

                        case WebBrowserUrl:
                            _webBrowserUrl = HttpUtility.UrlDecode(setting[1]);
                            break;

                        case EnableDjangoDebugging:
                            if (Boolean.TryParse(setting[1], out value) && value) {
                                _debugOptions |= PythonDebugOptions.DjangoDebugging;
                            }
                            break;
                    }
                }
            }
        }
コード例 #58
0
 public static IPythonModule FromTypeStub(
     IPythonInterpreter interpreter,
     string stubFile,
     PythonLanguageVersion langVersion,
     string moduleFullName
     ) => new AstCachedPythonModule(moduleFullName, stubFile);
コード例 #59
0
ファイル: PythonTestExtensions.cs プロジェクト: omnimark/PTVS
 public static void ParseFormat(this IPythonProjectEntry entry, PythonLanguageVersion version, string format, params object[] args) {
     entry.Parse(version, string.Format(format, args));
 }
コード例 #60
0
ファイル: PythonEditor.cs プロジェクト: scfunknown/PTVS
        public PythonEditor(
            string content = null,
            PythonLanguageVersion version = PythonLanguageVersion.V27,
            MockVs vs = null,
            IPythonInterpreterFactory factory = null,
            VsProjectAnalyzer analyzer        = null,
            string filename     = null,
            bool?inProcAnalyzer = null
            )
        {
            if (vs == null)
            {
                _disposeVS = true;
                vs         = new MockVs();
            }
            MockVsTextView view = null;

            try {
                AdvancedEditorOptions advancedOptions = null;
                vs.InvokeSync(() => {
                    advancedOptions = vs.GetPyService().AdvancedOptions;
                    advancedOptions.AutoListMembers     = true;
                    advancedOptions.AutoListIdentifiers = false;
                });
                AdvancedOptions = advancedOptions;

                if (factory == null)
                {
                    vs.InvokeSync(() => {
                        factory = vs.ComponentModel.GetService <IInterpreterRegistryService>()
                                  .Interpreters
                                  .FirstOrDefault(c => c.GetLanguageVersion() == version && c.Configuration.Id.StartsWith("Global|PythonCore"));
                        if (factory != null)
                        {
                            Console.WriteLine($"Using interpreter {factory.Configuration.InterpreterPath}");
                        }
                    });
                    if (factory == null)
                    {
                        _disposeFactory = true;
                        factory         = InterpreterFactoryCreator.CreateAnalysisInterpreterFactory(version.ToVersion());
                        Console.WriteLine("Using analysis-only interpreter");
                    }
                }
                if (analyzer == null)
                {
                    _disposeAnalyzer = true;
                    analyzer         = vs.InvokeTask(() => VsProjectAnalyzer.CreateForTestsAsync(vs.ComponentModel.GetService <PythonEditorServices>(), factory, inProcAnalyzer ?? Debugger.IsAttached), 10000);
                }
                if (string.IsNullOrEmpty(filename))
                {
                    do
                    {
                        filename = PathUtils.GetAbsoluteFilePath(TestData.GetTempPath(), Path.GetRandomFileName()) + ".py";
                    } while (File.Exists(filename));
                }

                var cancel = CancellationTokens.After60s;
                using (var mre = new ManualResetEventSlim()) {
                    view = vs.CreateTextView(PythonCoreConstants.ContentType, content ?? "",
                                             v => {
                        v.TextView.TextBuffer.Properties[BufferParser.ParseImmediately]             = true;
                        v.TextView.TextBuffer.Properties[IntellisenseController.SuppressErrorLists] = IntellisenseController.SuppressErrorLists;
                        v.TextView.TextBuffer.Properties[VsProjectAnalyzer._testAnalyzer]           = analyzer;
                        v.TextView.TextBuffer.Properties[VsProjectAnalyzer._testFilename]           = filename;
                    },
                                             filename);

                    var entry = analyzer.GetAnalysisEntryFromPath(filename);
                    while (entry == null && !cancel.IsCancellationRequested)
                    {
                        Thread.Sleep(50);
                        entry = analyzer.GetAnalysisEntryFromPath(filename);
                    }

                    if (!string.IsNullOrEmpty(content) && !cancel.IsCancellationRequested && !entry.IsAnalyzed)
                    {
                        EventHandler evt = (s, e) => mre.SetIfNotDisposed();

                        try {
                            entry.AnalysisComplete += evt;
                            while (!mre.Wait(50, cancel) && !vs.HasPendingException && !entry.IsAnalyzed)
                            {
                                if (!analyzer.IsAnalyzing && !entry.IsAnalyzed)
                                {
                                    var bp = entry.TryGetBufferParser();
                                    Assert.IsNotNull(bp, "No buffer parser was ever created");
                                    var bi = PythonTextBufferInfo.TryGetForBuffer(view.TextView.TextBuffer);
                                    Assert.IsNotNull(bi, "No BufferInfo was ever created");
                                    bi.LastSentSnapshot = null;
                                    bp.EnsureCodeSyncedAsync(view.TextView.TextBuffer).WaitAndUnwrapExceptions();
                                }
                            }
                        } catch (OperationCanceledException) {
                        } finally {
                            entry.AnalysisComplete -= evt;
                        }
                    }
                    if (cancel.IsCancellationRequested)
                    {
                        Assert.Fail("Timed out waiting for code analysis");
                    }

                    vs.ThrowPendingException();
                }

                View     = view;
                view     = null;
                Analyzer = analyzer;
                analyzer = null;
                Factory  = factory;
                factory  = null;
                VS       = vs;
                vs       = null;
            } finally {
                if (view != null)
                {
                    view.Dispose();
                }
                if (analyzer != null && _disposeAnalyzer)
                {
                    analyzer.Dispose();
                }
                if (factory != null && _disposeFactory)
                {
                    var disp = factory as IDisposable;
                    if (disp != null)
                    {
                        disp.Dispose();
                    }
                }
                if (vs != null && _disposeVS)
                {
                    vs.Dispose();
                }
            }
        }