14 |
14 |
15 from Tools import readEncodedFile, normalizeCode |
15 from Tools import readEncodedFile, normalizeCode |
16 |
16 |
17 from py2flakes.checker import Checker |
17 from py2flakes.checker import Checker |
18 from py2flakes.messages import ImportStarUsed |
18 from py2flakes.messages import ImportStarUsed |
|
19 |
19 |
20 |
20 def compile(file, codestring): |
21 def compile(file, codestring): |
21 """ |
22 """ |
22 Function to compile one Python source file to Python bytecode. |
23 Function to compile one Python source file to Python bytecode. |
23 |
24 |
43 template.compile() |
44 template.compile() |
44 else: |
45 else: |
45 __builtin__.compile(codestring, file, 'exec') |
46 __builtin__.compile(codestring, file, 'exec') |
46 except SyntaxError, detail: |
47 except SyntaxError, detail: |
47 lines = traceback.format_exception_only(SyntaxError, detail) |
48 lines = traceback.format_exception_only(SyntaxError, detail) |
48 match = re.match('\s*File "(.+)", line (\d+)', |
49 match = re.match('\s*File "(.+)", line (\d+)', |
49 lines[0].replace('<string>', '%s' % file)) |
50 lines[0].replace('<string>', '%s' % file)) |
50 if match is not None: |
51 if match is not None: |
51 fn, line = match.group(1, 2) |
52 fn, line = match.group(1, 2) |
52 if lines[1].startswith('SyntaxError:'): |
53 if lines[1].startswith('SyntaxError:'): |
53 code = "" |
54 code = "" |
91 except: # this catchall is intentional |
92 except: # this catchall is intentional |
92 pass |
93 pass |
93 |
94 |
94 return (0, None, None, None, None, None) |
95 return (0, None, None, None, None, None) |
95 |
96 |
|
97 |
96 def flakesCheck(fileName, codestring, ignoreStarImportWarnings): |
98 def flakesCheck(fileName, codestring, ignoreStarImportWarnings): |
97 """ |
99 """ |
98 Function to perform a pyflakes check. |
100 Function to perform a pyflakes check. |
99 |
101 |
100 @param fileName name of the file (string) |
102 @param fileName name of the file (string) |
101 @param codestring source code to be checked (string) |
103 @param codestring source code to be checked (string) |
102 @param ignoreStarImportWarnings flag indicating to |
104 @param ignoreStarImportWarnings flag indicating to |
103 ignore 'star import' warnings (boolean) |
105 ignore 'star import' warnings (boolean) |
104 @return list of strings containing the warnings |
106 @return list of strings containing the warnings |
105 (marker, file name, line number, message) |
107 (marker, file name, line number, message) |
106 """ |
108 """ |
107 strings = [] |
109 strings = [] |
108 lines = codestring.splitlines() |
110 lines = codestring.splitlines() |
109 try: |
111 try: |
110 warnings = Checker(codestring, fileName) |
112 warnings = Checker(codestring, fileName) |
111 warnings.messages.sort(key = lambda a: a.lineno) |
113 warnings.messages.sort(key=lambda a: a.lineno) |
112 for warning in warnings.messages: |
114 for warning in warnings.messages: |
113 if ignoreStarImportWarnings and \ |
115 if ignoreStarImportWarnings and \ |
114 isinstance(warning, ImportStarUsed): |
116 isinstance(warning, ImportStarUsed): |
115 continue |
117 continue |
116 |
118 |