forked from dhvanipa/error_deep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_pypy_syntax.py
91 lines (73 loc) · 2.65 KB
/
check_pypy_syntax.py
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
#!/usr/bin/python
# Copyright 2017 Dhvani Patel
#
# This file is part of UnnaturalCode.
#
# UnnaturalCode is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# UnnaturalCode is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with UnnaturalCode. If not, see <http://www.gnu.org/licenses/>.
# Takes in a string of Python code and checks for errors
# NOTE: FOR PYPY
import os
import subprocess
import sys
import tempfile
from compile_error import CompileError
import re
# Method for finding index of certain characters in a string, n being the n'th occurence of the character/string
def find_nth(haystack, needle, n):
start = haystack.find(needle.encode())
while start >= 0 and n > 1:
start = haystack.find(needle, start+len(needle))
n -= 1
return start
# Main method
def checkPyPySyntax(src):
myFile = open("toCheck.py", "w")
myFile.write(src.decode())
myFile.close()
proc = subprocess.Popen(['pypy', '-m', 'py_compile', 'toCheck.py'], stderr=subprocess.PIPE)
streamdata, err = proc.communicate()
rc = proc.returncode
if rc == 0:
# No errors, all good
if os.path.isfile("toCheck.py") == True:
os.remove("toCheck.py")
return None
else:
# Error, disect data for constructor
fileBegInd = find_nth(err, 'File ', 1)
fileEndInd = find_nth(err, ',', 1)
lineInd = find_nth(err, 'line ', 1)
nextLineInd = find_nth(err, '\n', 1)
add = err[lineInd+5:nextLineInd]
add = re.sub("[^0-9]", "", add)
if(add == ''):
add = '-1'
line = int(add)
textInd = find_nth(err, ' ', 1)
temp2 = err[textInd+4:]
nextLineIndTemp = find_nth(temp2, ' ', 1)
textAfter = err[textInd+4:nextLineIndTemp+textInd+3]
fileName = err[fileBegInd+6:fileEndInd-1]
colon = ':'
textBeforeInd = err.rfind(colon.encode())
textBefore = err[textBeforeInd+2:]
textBefore = textBefore.strip()
colonTwo = ':'
text = textBefore + colon.encode() + textAfter
cutoffInd = find_nth(err, '^', 1)
errorname = err[cutoffInd+2:textBeforeInd]
errorObj = CompileError(fileName, line, None, None, text, errorname)
if os.path.isfile("toCheck.py") == True:
os.remove("toCheck.py")
return [errorObj]