-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPythonErrorAnalyser.cs
More file actions
459 lines (398 loc) · 14.2 KB
/
Copy pathPythonErrorAnalyser.cs
File metadata and controls
459 lines (398 loc) · 14.2 KB
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
// PythonErrorAnalyzer.cs
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Microsoft.Scripting.Hosting;
using IronPython.Hosting;
using Microsoft.VisualStudio.Services.Common;
namespace RYCBEditorX.Utils;
public static class PythonErrorAnalyzer
{
private static readonly ScriptEngine PythonEngine = Python.CreateEngine();
public static List<PyCodeErr> AnalyzeCode(string code)
{
var errors = new List<PyCodeErr>();
// 1. 语法检查
errors.AddRange(CheckSyntaxErrors(code));
// 2. 只有语法正确时才进行静态分析
if (errors.Count == 0)
{
errors.AddRange(CheckStaticIssues(code));
}
return errors;
}
private static List<PyCodeErr> CheckStaticIssues(string code)
{
var errors = new List<PyCodeErr>();
// 检查未定义变量
var varRegex = new Regex(@"(?<!\w\.)(?<var>[a-zA-Z_]\w*)(?!\s*=)[^\.\w]");
foreach (Match match in varRegex.Matches(code))
{
var varName = match.Groups["var"].Value;
if (!IsBuiltin(varName) && !IsDefinedInCode(code, varName, match.Index))
{
int lineNum = GetLineNumber(code, match.Index);
errors.Add(new PyCodeErr
{
Type = PyCodeErr.ErrorType.NameError,
Message = $"可能未定义的变量: {varName}",
LineNumber = lineNum,
CodeSnippet = GetLine(code, lineNum),
FilePath = "当前编辑的文件"
});
}
}
return errors;
}
private static bool IsBuiltin(string name)
{
var builtins = new HashSet<string>
{
"print", "range", "len", "str", "int", "list", "dict",
"True", "False", "None", "and", "or", "not", "in", "is",
"def", "class", "import", "from", "return", "if", "else",
"elif", "for", "while", "try", "except", "finally", "with"
};
builtins.AddRange(Language.Python.BuiltIns);
builtins.AddRange(Language.Python.Keywords);
builtins.AddRange(Language.Python.MagicMethods);
return builtins.Contains(name);
}
private static bool IsDefinedInCode(string code, string varName, int beforeIndex)
{
// 检查变量是否在指定位置前定义过
var definitionPattern = $@"(def|class)\s+{varName}\b|{varName}\s*=";
return Regex.IsMatch(code.Substring(0, beforeIndex), definitionPattern);
}
private static string GetLine(string code, int lineNumber)
{
if (string.IsNullOrEmpty(code) || lineNumber < 1)
return string.Empty;
using (var reader = new StringReader(code))
{
string line;
int currentLine = 1;
while ((line = reader.ReadLine()) != null)
{
if (currentLine == lineNumber)
return line.Trim();
currentLine++;
}
}
return string.Empty;
}
private static int GetLineNumber(string code, int charIndex)
{
if (string.IsNullOrEmpty(code) || charIndex < 0)
return 1;
int lineNumber = 1;
for (int i = 0; i < Math.Min(charIndex, code.Length); i++)
{
if (code[i] == '\n')
lineNumber++;
}
return lineNumber;
}
public static async Task<List<PyCodeErr>> AnalyzePythonFileAsync(string filePath)
{
return await Task.Run(() =>
{
var errors = new List<PyCodeErr>();
if (!File.Exists(filePath))
{
errors.Add(new PyCodeErr
{
Type = PyCodeErr.ErrorType.OtherError,
Message = "File not found",
LineNumber = 0,
FilePath = filePath,
CodeSnippet = string.Empty
});
return errors;
}
// 1. 首先检查语法错误
var syntaxErrors = CheckSyntaxErrors(filePath);
if (syntaxErrors.Count > 0)
{
return syntaxErrors;
}
// 2. 检查运行时错误
var runtimeErrors = CheckRuntimeErrors(filePath);
errors.AddRange(runtimeErrors);
return errors;
});
}
public static List<PyCodeErr> AnalyzePythonFile(string filePath)
{
var errors = new List<PyCodeErr>();
if (!File.Exists(filePath))
{
errors.Add(new PyCodeErr
{
Type = PyCodeErr.ErrorType.OtherError,
Message = "File not found",
LineNumber = 0,
FilePath = filePath,
CodeSnippet = string.Empty
});
return errors;
}
// 1. 首先检查语法错误
var syntaxErrors = CheckSyntaxErrors(filePath);
if (syntaxErrors.Count > 0)
{
return syntaxErrors; // 如果有语法错误,直接返回,因为无法运行
}
// 2. 检查运行时错误
var runtimeErrors = CheckRuntimeErrors(filePath);
errors.AddRange(runtimeErrors);
return errors;
}
private static List<PyCodeErr> CheckSyntaxErrors(string filePath)
{
var errors = new List<PyCodeErr>();
var pythonExe = FindPythonExecutable();
if (pythonExe == null)
{
errors.Add(new PyCodeErr
{
Type = PyCodeErr.ErrorType.OtherError,
Message = "Python executable not found",
LineNumber = 0,
FilePath = filePath,
CodeSnippet = string.Empty
});
return errors;
}
var processStartInfo = new ProcessStartInfo
{
FileName = pythonExe,
Arguments = $"-m py_compile \"{filePath}\"",
RedirectStandardError = true,
UseShellExecute = false,
CreateNoWindow = true,
StandardErrorEncoding = Encoding.UTF8
};
using var process = Process.Start(processStartInfo);
var errorOutput = process.StandardError.ReadToEnd();
process.WaitForExit(5000); // 等待最多5秒
if (!string.IsNullOrEmpty(errorOutput))
{
var syntaxError = ParsePythonError(errorOutput, filePath);
if (syntaxError != null)
{
errors.Add(syntaxError);
}
}
return errors;
}
private static List<PyCodeErr> CheckRuntimeErrors(string filePath)
{
var errors = new List<PyCodeErr>();
var pythonExe = FindPythonExecutable();
if (pythonExe == null)
{
errors.Add(new PyCodeErr
{
Type = PyCodeErr.ErrorType.OtherError,
Message = "Python executable not found",
LineNumber = 0,
FilePath = filePath,
CodeSnippet = string.Empty
});
return errors;
}
var processStartInfo = new ProcessStartInfo
{
FileName = pythonExe,
Arguments = $"\"{filePath}\"",
RedirectStandardError = true,
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true,
StandardErrorEncoding = Encoding.UTF8,
StandardOutputEncoding = Encoding.UTF8
};
using var process = Process.Start(processStartInfo);
var errorOutput = process.StandardError.ReadToEnd();
var stdOutput = process.StandardOutput.ReadToEnd();
process.WaitForExit(5000); // 等待最多5秒
if (!string.IsNullOrEmpty(errorOutput))
{
var runtimeErrors = ParseRuntimeErrors(errorOutput, filePath);
errors.AddRange(runtimeErrors);
}
return errors;
}
private static PyCodeErr ParsePythonError(string errorOutput, string filePath)
{
// 示例错误格式:
// File "test.py", line 2
// print("Hello world"
// ^
// SyntaxError: unexpected EOF while parsing
var lines = errorOutput.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
if (lines.Length < 2) return null;
// 解析第一行获取文件名和行号
var firstLineMatch = Regex.Match(lines[0], @"File ""(.+)"", line (\d+)");
if (!firstLineMatch.Success) return null;
var lineNumber = int.Parse(firstLineMatch.Groups[2].Value);
var codeSnippet = lines.Length > 1 ? lines[1].Trim() : string.Empty;
// 解析错误类型和消息
var errorType = PyCodeErr.ErrorType.SyntaxError;
var errorMessage = lines[^1].Trim();
if (lines.Length > 2)
{
var lastLine = lines[^1];
if (lastLine.StartsWith("SyntaxError:"))
{
errorMessage = lastLine["SyntaxError:".Length..].Trim();
}
else if (lastLine.StartsWith("IndentationError:"))
{
errorMessage = lastLine["IndentationError:".Length..].Trim();
errorType = PyCodeErr.ErrorType.SyntaxError;
}
}
return new PyCodeErr
{
Type = errorType,
Message = errorMessage,
LineNumber = lineNumber,
FilePath = filePath,
CodeSnippet = codeSnippet
};
}
private static List<PyCodeErr> ParseRuntimeErrors(string errorOutput, string filePath)
{
var errors = new List<PyCodeErr>();
var lines = errorOutput.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
if (lines.Length == 0) return errors;
// 示例运行时错误格式:
// Traceback (most recent call last):
// File "test.py", line 3, in <module>
// print(undefined_var)
// NameError: name 'undefined_var' is not defined
var tracebackStart = -1;
for (int i = 0; i < lines.Length; i++)
{
if (lines[i].StartsWith("Traceback (most recent call last):"))
{
tracebackStart = i;
break;
}
}
if (tracebackStart == -1)
{
// 不是标准traceback格式的错误
errors.Add(new PyCodeErr
{
Type = PyCodeErr.ErrorType.OtherError,
Message = errorOutput.Trim(),
LineNumber = 0,
FilePath = filePath,
CodeSnippet = string.Empty
});
return errors;
}
// 解析traceback
if (tracebackStart + 2 >= lines.Length) return errors;
// 获取错误发生的位置
var fileLineMatch = Regex.Match(lines[tracebackStart + 1], @"File ""(.+)"", line (\d+), in (.+)");
if (!fileLineMatch.Success) return errors;
var lineNumber = int.Parse(fileLineMatch.Groups[2].Value);
var codeSnippet = tracebackStart + 2 < lines.Length ? lines[tracebackStart + 2].Trim() : string.Empty;
// 获取错误类型和消息
var errorLine = lines[^1];
PyCodeErr.ErrorType errorType;
string errorMessage;
if (errorLine.StartsWith("NameError:"))
{
errorType = PyCodeErr.ErrorType.NameError;
errorMessage = errorLine["NameError:".Length..].Trim();
}
else if (errorLine.StartsWith("TypeError:"))
{
errorType = PyCodeErr.ErrorType.TypeError;
errorMessage = errorLine["TypeError:".Length..].Trim();
}
else if (errorLine.StartsWith("ValueError:"))
{
errorType = PyCodeErr.ErrorType.ValueError;
errorMessage = errorLine["ValueError:".Length..].Trim();
}
else if (errorLine.StartsWith("ImportError:"))
{
errorType = PyCodeErr.ErrorType.ImportError;
errorMessage = errorLine["ImportError:".Length..].Trim();
}
else if (errorLine.StartsWith("RuntimeError:"))
{
errorType = PyCodeErr.ErrorType.RuntimeError;
errorMessage = errorLine["RuntimeError:".Length..].Trim();
}
else
{
errorType = PyCodeErr.ErrorType.OtherError;
errorMessage = errorLine;
}
errors.Add(new PyCodeErr
{
Type = errorType,
Message = errorMessage,
LineNumber = lineNumber,
FilePath = filePath,
CodeSnippet = codeSnippet
});
return errors;
}
private static string FindPythonExecutable()
{
if (File.Exists(GlobalConfig.PythonPath)) return GlobalConfig.PythonPath;
// 检查常见Python安装路径
var possiblePaths = new List<string>
{
"python", // 如果在PATH中
"python3",
"py",
@"C:\Python310\python.exe",
@"C:\Python311\python.exe",
@"C:\Python312\python.exe",
@"C:\Python313\python.exe",
@"C:\Program Files\Python310\python.exe",
@"C:\Program Files\Python312\python.exe",
@"C:\Program Files\Python311\python.exe",
@"C:\Program Files\Python313\python.exe",
};
foreach (var path in possiblePaths)
{
try
{
var processStartInfo = new ProcessStartInfo
{
FileName = path,
Arguments = "--version",
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
CreateNoWindow = true
};
using var process = Process.Start(processStartInfo);
process.WaitForExit(1000); // 等待1秒
if (process.ExitCode == 0)
{
return path;
}
}
catch
{
// 忽略异常,继续尝试下一个路径
}
}
return null;
}
}