UtilitiesPython2/Py2SyntaxChecker.py

changeset 1465
3b459b88fcd4
parent 1308
2b602a1521e7
child 1509
c0b5e693b0eb
equal deleted inserted replaced
1464:e4ec683781e4 1465:3b459b88fcd4
43 template = quixote.ptl_compile.Template(codestring, file) 43 template = quixote.ptl_compile.Template(codestring, file)
44 template.compile() 44 template.compile()
45 else: 45 else:
46 __builtin__.compile(codestring, file, 'exec') 46 __builtin__.compile(codestring, file, 'exec')
47 except SyntaxError, detail: 47 except SyntaxError, detail:
48 index = "0"
49 code = ""
50 error = ""
48 lines = traceback.format_exception_only(SyntaxError, detail) 51 lines = traceback.format_exception_only(SyntaxError, detail)
49 match = re.match('\s*File "(.+)", line (\d+)', 52 match = re.match('\s*File "(.+)", line (\d+)',
50 lines[0].replace('<string>', '%s' % file)) 53 lines[0].replace('<string>', '%s' % file))
51 if match is not None: 54 if match is not None:
52 fn, line = match.group(1, 2) 55 fn, line = match.group(1, 2)
53 if lines[1].startswith('SyntaxError:'): 56 if lines[1].startswith('SyntaxError:'):
54 code = ""
55 error = re.match('SyntaxError: (.+)', lines[1]).group(1) 57 error = re.match('SyntaxError: (.+)', lines[1]).group(1)
56 index = "0"
57 else: 58 else:
58 code = re.match('(.+)', lines[1]).group(1) 59 code = re.match('(.+)', lines[1]).group(1)
59 error = ""
60 index = "0"
61 for seLine in lines[2:]: 60 for seLine in lines[2:]:
62 if seLine.startswith('SyntaxError:'): 61 if seLine.startswith('SyntaxError:'):
63 error = re.match('SyntaxError: (.+)', seLine).group(1) 62 error = re.match('SyntaxError: (.+)', seLine).group(1)
64 elif seLine.rstrip().endswith('^'): 63 elif seLine.rstrip().endswith('^'):
65 index = len(seLine.rstrip()) - 4 64 index = len(seLine.rstrip()) - 4
66 else: 65 else:
67 fn = detail.filename 66 fn = detail.filename
68 line = detail.lineno and detail.lineno or 1 67 line = detail.lineno and detail.lineno or 1
69 code = ""
70 error = detail.msg 68 error = detail.msg
71 return (1, fn, line, index, code, error) 69 return (1, fn, line, index, code, error)
72 except ValueError, detail: 70 except ValueError, detail:
73 index = "0" 71 index = "0"
72 code = ""
74 try: 73 try:
75 fn = detail.filename 74 fn = detail.filename
76 line = detail.lineno 75 line = detail.lineno
77 error = detail.msg 76 error = detail.msg
78 except AttributeError: 77 except AttributeError:
79 fn = file 78 fn = file
80 line = 1 79 line = 1
81 error = unicode(detail) 80 error = unicode(detail)
82 code = ""
83 return (1, fn, line, index, code, error) 81 return (1, fn, line, index, code, error)
84 except StandardError, detail: 82 except StandardError, detail:
85 try: 83 try:
86 fn = detail.filename 84 fn = detail.filename
87 line = detail.lineno 85 line = detail.lineno
86 index = "0"
88 code = "" 87 code = ""
89 error = detail.msg 88 error = detail.msg
90 index = "0"
91 return (1, fn, line, index, code, error) 89 return (1, fn, line, index, code, error)
92 except: # this catchall is intentional 90 except: # this catchall is intentional
93 pass 91 pass
94 92
95 return (0, None, None, None, None, None) 93 return (0, None, None, None, None, None)

eric ide

mercurial