forked from microsoft/python-language-server
/
PythonInterpreter.cs
163 lines (142 loc) · 6.1 KB
/
PythonInterpreter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// Copyright(c) Microsoft Corporation
// All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the License); you may not use
// this file except in compliance with the License. You may obtain a copy of the
// License at http://www.apache.org/licenses/LICENSE-2.0
//
// THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS
// OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY
// IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
// MERCHANTABILITY OR NON-INFRINGEMENT.
//
// See the Apache Version 2.0 License for specific language governing
// permissions and limitations under the License.
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Python.Analysis.Core.Interpreter;
using Microsoft.Python.Analysis.Modules;
using Microsoft.Python.Analysis.Modules.Resolution;
using Microsoft.Python.Analysis.Specializations.Typing;
using Microsoft.Python.Analysis.Types;
using Microsoft.Python.Analysis.Values;
using Microsoft.Python.Core;
using Microsoft.Python.Core.Collections;
using Microsoft.Python.Core.Services;
using Microsoft.Python.Parsing;
namespace Microsoft.Python.Analysis.Analyzer {
/// <summary>
/// Describes Python interpreter associated with the analysis.
/// </summary>
public sealed class PythonInterpreter : IPythonInterpreter {
private MainModuleResolution _moduleResolution;
private TypeshedResolution _stubResolution;
private IPythonType _unknownType;
private readonly object _lock = new object();
private readonly Dictionary<BuiltinTypeId, IPythonType> _builtinTypes = new Dictionary<BuiltinTypeId, IPythonType>();
private PythonInterpreter(InterpreterConfiguration configuration) {
Configuration = configuration ?? throw new ArgumentNullException(nameof(configuration));
LanguageVersion = Configuration.Version.ToLanguageVersion();
}
private async Task InitializeAsync(
string root,
IServiceManager sm,
string typeshedPath,
ImmutableArray<string> userConfiguredPaths,
CancellationToken cancellationToken
) {
cancellationToken.ThrowIfCancellationRequested();
sm.AddService(this);
_moduleResolution = new MainModuleResolution(root, sm, userConfiguredPaths);
_stubResolution = new TypeshedResolution(typeshedPath, sm);
await _stubResolution.ReloadAsync(cancellationToken);
await _moduleResolution.ReloadAsync(cancellationToken);
}
public static async Task<IPythonInterpreter> CreateAsync(
InterpreterConfiguration configuration,
string root,
IServiceManager sm,
string typeshedPath = null,
ImmutableArray<string> userConfiguredPaths = default,
CancellationToken cancellationToken = default
) {
var pi = new PythonInterpreter(configuration);
await pi.InitializeAsync(root, sm, typeshedPath, userConfiguredPaths, cancellationToken);
// Specialize typing
TypingModule.Create(sm);
return pi;
}
/// <summary>
/// Interpreter configuration.
/// </summary>
public InterpreterConfiguration Configuration { get; }
/// <summary>
/// Python language version.
/// </summary>
public PythonLanguageVersion LanguageVersion { get; }
/// <summary>
/// Module resolution service.
/// </summary>
public IModuleManagement ModuleResolution => _moduleResolution;
/// <summary>
/// Stub resolution service.
/// </summary>
public IModuleResolution TypeshedResolution => _stubResolution;
/// <summary>
/// Unknown type.
/// </summary>
public IPythonType UnknownType {
get {
lock (_lock) {
var type = _unknownType;
if (type != null) {
return type;
}
_unknownType = new PythonType("Unknown", new Location(_moduleResolution.BuiltinsModule), string.Empty);
_builtinTypes[BuiltinTypeId.Unknown] = _unknownType;
return _unknownType;
}
}
}
/// <summary>
/// Gets a well known built-in type such as int, list, dict, etc...
/// </summary>
/// <param name="id">The built-in type to get</param>
/// <returns>An IPythonType representing the type.</returns>
/// <exception cref="KeyNotFoundException">
/// The requested type cannot be resolved by this interpreter.
/// </exception>
public IPythonType GetBuiltinType(BuiltinTypeId id) {
if (id < 0 || id > BuiltinTypeIdExtensions.LastTypeId) {
throw new KeyNotFoundException("(BuiltinTypeId)({0})".FormatInvariant((int)id));
}
lock (_lock) {
if (id == BuiltinTypeId.Unknown) {
return UnknownType;
}
if (_builtinTypes.TryGetValue(id, out var type) && type != null) {
return type;
}
if (id == BuiltinTypeId.NoneType) {
type = new PythonNone(_moduleResolution.BuiltinsModule);
} else {
var bm = _moduleResolution.BuiltinsModule;
var typeName = id.GetTypeName(LanguageVersion);
if (typeName != null) {
type = _moduleResolution.BuiltinsModule.GetMember(typeName) as IPythonType;
}
if (type == null) {
type = bm.GetAnyMember("__{0}__".FormatInvariant(id)) as IPythonType;
if (type == null) {
return UnknownType;
}
}
}
_builtinTypes[id] = type;
return type;
}
}
}
}