Python tokenize.TokenError() Examples
The following are 30
code examples of tokenize.TokenError().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
tokenize
, or try the search function
.
Example #1
Source File: ScriptBinding.py From ironpython3 with Apache License 2.0 | 6 votes |
def tabnanny(self, filename): # XXX: tabnanny should work on binary files as well with tokenize.open(filename) as f: try: tabnanny.process_tokens(tokenize.generate_tokens(f.readline)) except tokenize.TokenError as msg: msgtxt, (lineno, start) = msg.args self.editwin.gotoline(lineno) self.errorbox("Tabnanny Tokenizing Error", "Token Error: %s" % msgtxt) return False except tabnanny.NannyNag as nag: # The error messages from tabnanny are too confusing... self.editwin.gotoline(nag.get_lineno()) self.errorbox("Tab/space error", indent_message) return False return True
Example #2
Source File: processor.py From linter-pylama with MIT License | 6 votes |
def file_tokens(self): """The complete set of tokens for a file. Accessing this attribute *may* raise an InvalidSyntax exception. :raises: flake8.exceptions.InvalidSyntax """ if self._file_tokens is None: line_iter = iter(self.lines) try: self._file_tokens = list(tokenize.generate_tokens( lambda: next(line_iter) )) except tokenize.TokenError as exc: raise exceptions.InvalidSyntax(exc.message, exception=exc) return self._file_tokens
Example #3
Source File: autopep8.py From python-netsurv with MIT License | 6 votes |
def fix_long_line_physically(self, result): """Try to make lines fit within --max-line-length characters.""" line_index = result['line'] - 1 target = self.source[line_index] previous_line = get_item(self.source, line_index - 1, default='') next_line = get_item(self.source, line_index + 1, default='') try: fixed = self.fix_long_line( target=target, previous_line=previous_line, next_line=next_line, original=target) except (SyntaxError, tokenize.TokenError): return [] if fixed: self.source[line_index] = fixed return [line_index + 1] return []
Example #4
Source File: raw.py From linter-pylama with MIT License | 6 votes |
def _get_all_tokens(line, lines): '''Starting from *line*, generate the necessary tokens which represent the shortest tokenization possible. This is done by catching :exc:`tokenize.TokenError` when a multi-line string or statement is encountered. :returns: tokens, lines ''' buffer = line used_lines = [line] while True: try: tokens = _generate(buffer) except tokenize.TokenError: # A multi-line string or statement has been encountered: # start adding lines and stop when tokenize stops complaining pass else: if not any(t[0] == tokenize.ERRORTOKEN for t in tokens): return tokens, used_lines # Add another line next_line = next(lines) buffer = buffer + '\n' + next_line used_lines.append(next_line)
Example #5
Source File: config_scope.py From sacred with MIT License | 6 votes |
def find_doc_for(ast_entry, body_lines): lineno = ast_entry.lineno - 1 line_io = io.BytesIO(body_lines[lineno].encode()) try: tokens = tokenize(line_io.readline) or [] line_comments = [t.string for t in tokens if t.type == COMMENT] if line_comments: formatted_lcs = [l[1:].strip() for l in line_comments] filtered_lcs = [l for l in formatted_lcs if not is_ignored(l)] if filtered_lcs: return filtered_lcs[0] except TokenError: pass lineno -= 1 while lineno >= 0: if iscomment(body_lines[lineno]): comment = body_lines[lineno].strip("# ") if not is_ignored(comment): return comment if not body_lines[lineno].strip() == "": return None lineno -= 1 return None
Example #6
Source File: importer.py From importmagic with BSD 2-Clause "Simplified" License | 6 votes |
def _parse(self, source): reader = StringIO(source) # parse until EOF or TokenError (allows incomplete modules) tokens = [] try: tokens.extend(tokenize.generate_tokens(reader.readline)) except tokenize.TokenError: # TokenError happens always at EOF, for unclosed strings or brackets. # We don't care about that here, since we still can recover the whole # source code. pass self._tokens = tokens it = Iterator(self._tokens) self._imports_begin, self._imports_end = self._find_import_range(it) it = Iterator(self._tokens, start=self._imports_begin, end=self._imports_end) self._parse_imports(it)
Example #7
Source File: tbtools.py From recruit with Apache License 2.0 | 6 votes |
def get_annotated_lines(self): """Helper function that returns lines with extra information.""" lines = [Line(idx + 1, x) for idx, x in enumerate(self.sourcelines)] # find function definition and mark lines if hasattr(self.code, "co_firstlineno"): lineno = self.code.co_firstlineno - 1 while lineno > 0: if _funcdef_re.match(lines[lineno].code): break lineno -= 1 try: offset = len(inspect.getblock([x.code + "\n" for x in lines[lineno:]])) except TokenError: offset = 0 for line in lines[lineno : lineno + offset]: line.in_frame = True # mark current line try: lines[self.lineno - 1].current = True except IndexError: pass return lines
Example #8
Source File: autopep8.py From python-netsurv with MIT License | 6 votes |
def fix_e402(self, result): (line_index, offset, target) = get_index_offset_contents(result, self.source) for i in range(1, 100): line = "".join(self.source[line_index:line_index+i]) try: generate_tokens("".join(line)) except (SyntaxError, tokenize.TokenError): continue break if not (target in self.imports and self.imports[target] != line_index): mod_offset = get_module_imports_on_top_of_file(self.source, line_index) self.source[mod_offset] = line + self.source[mod_offset] for offset in range(i): self.source[line_index+offset] = ''
Example #9
Source File: __init__.py From pyta with GNU General Public License v3.0 | 6 votes |
def _verify_pre_check(filepath): """Check student code for certain issues.""" # Make sure the program doesn't crash for students. # Could use some improvement for better logging and error reporting. try: # Check for inline "pylint:" comment, which may indicate a student # trying to disable a check. with tokenize.open(os.path.expanduser(filepath)) as f: for tok_type, content, _, _, _ in tokenize.generate_tokens(f.readline): if tok_type != tokenize.COMMENT: continue match = pylint.constants.OPTION_RGX.search(content) if match is not None: print('[ERROR] String "pylint:" found in comment. ' + 'No check run on file `{}.`\n'.format(filepath)) return False except IndentationError as e: print('[ERROR] python_ta could not check your code due to an ' + 'indentation error at line {}.'.format(e.lineno)) return False except tokenize.TokenError as e: print('[ERROR] python_ta could not check your code due to a ' + 'syntax error in your file.') return False return True
Example #10
Source File: source.py From py with MIT License | 6 votes |
def deindent(lines, offset=None): if offset is None: for line in lines: line = line.expandtabs() s = line.lstrip() if s: offset = len(line)-len(s) break else: offset = 0 if offset == 0: return list(lines) newlines = [] def readline_generator(lines): for line in lines: yield line + '\n' while True: yield '' it = readline_generator(lines) try: for _, _, (sline, _), (eline, _), _ in tokenize.generate_tokens(lambda: next(it)): if sline > len(lines): break # End of input reached if sline > len(newlines): line = lines[sline - 1].expandtabs() if line.lstrip() and line[:offset].isspace(): line = line[offset:] # Deindent newlines.append(line) for i in range(sline, eline): # Don't deindent continuing lines of # multiline tokens (i.e. multiline strings) newlines.append(lines[i]) except (IndentationError, tokenize.TokenError): pass # Add any lines we didn't see. E.g. if an exception was raised. newlines.extend(lines[len(newlines):]) return newlines
Example #11
Source File: autopep8.py From python-netsurv with MIT License | 6 votes |
def refactor_with_2to3(source_text, fixer_names, filename=''): """Use lib2to3 to refactor the source. Return the refactored source code. """ from lib2to3.refactor import RefactoringTool fixers = ['lib2to3.fixes.fix_' + name for name in fixer_names] tool = RefactoringTool(fixer_names=fixers, explicit=fixers) from lib2to3.pgen2 import tokenize as lib2to3_tokenize try: # The name parameter is necessary particularly for the "import" fixer. return unicode(tool.refactor_string(source_text, name=filename)) except lib2to3_tokenize.TokenError: return source_text
Example #12
Source File: autopep8.py From python-netsurv with MIT License | 6 votes |
def fix_e402(self, result): (line_index, offset, target) = get_index_offset_contents(result, self.source) for i in range(1, 100): line = "".join(self.source[line_index:line_index+i]) try: generate_tokens("".join(line)) except (SyntaxError, tokenize.TokenError): continue break if not (target in self.imports and self.imports[target] != line_index): mod_offset = get_module_imports_on_top_of_file(self.source, line_index) self.source[mod_offset] = line + self.source[mod_offset] for offset in range(i): self.source[line_index+offset] = ''
Example #13
Source File: autopep8.py From python-netsurv with MIT License | 6 votes |
def fix_long_line_physically(self, result): """Try to make lines fit within --max-line-length characters.""" line_index = result['line'] - 1 target = self.source[line_index] previous_line = get_item(self.source, line_index - 1, default='') next_line = get_item(self.source, line_index + 1, default='') try: fixed = self.fix_long_line( target=target, previous_line=previous_line, next_line=next_line, original=target) except (SyntaxError, tokenize.TokenError): return [] if fixed: self.source[line_index] = fixed return [line_index + 1] return []
Example #14
Source File: autopep8.py From python-netsurv with MIT License | 6 votes |
def refactor_with_2to3(source_text, fixer_names, filename=''): """Use lib2to3 to refactor the source. Return the refactored source code. """ from lib2to3.refactor import RefactoringTool fixers = ['lib2to3.fixes.fix_' + name for name in fixer_names] tool = RefactoringTool(fixer_names=fixers, explicit=fixers) from lib2to3.pgen2 import tokenize as lib2to3_tokenize try: # The name parameter is necessary particularly for the "import" fixer. return unicode(tool.refactor_string(source_text, name=filename)) except lib2to3_tokenize.TokenError: return source_text
Example #15
Source File: AutoIndent.py From ironpython2 with Apache License 2.0 | 6 votes |
def run(self): OPENERS=('class', 'def', 'for', 'if', 'try', 'while') INDENT=tokenize.INDENT NAME=tokenize.NAME save_tabsize = tokenize.tabsize tokenize.tabsize = self.tabwidth try: try: for (typ, token, start, end, line) in token_generator(self.readline): if typ == NAME and token in OPENERS: self.blkopenline = line elif type == INDENT and self.blkopenline: self.indentedline = line break except (tokenize.TokenError, IndentationError): # since we cut off the tokenizer early, we can trigger # spurious errors pass finally: tokenize.tabsize = save_tabsize return self.blkopenline, self.indentedline
Example #16
Source File: ScriptBinding.py From oss-ftp with MIT License | 6 votes |
def tabnanny(self, filename): f = open(filename, 'r') try: tabnanny.process_tokens(tokenize.generate_tokens(f.readline)) except tokenize.TokenError as msg: msgtxt, (lineno, start) = msg self.editwin.gotoline(lineno) self.errorbox("Tabnanny Tokenizing Error", "Token Error: %s" % msgtxt) return False except tabnanny.NannyNag as nag: # The error messages from tabnanny are too confusing... self.editwin.gotoline(nag.get_lineno()) self.errorbox("Tab/space error", indent_message) return False return True
Example #17
Source File: autopep8.py From PyDev.Debugger with Eclipse Public License 1.0 | 6 votes |
def fix_long_line_physically(self, result): """Try to make lines fit within --max-line-length characters.""" line_index = result['line'] - 1 target = self.source[line_index] previous_line = get_item(self.source, line_index - 1, default='') next_line = get_item(self.source, line_index + 1, default='') try: fixed = self.fix_long_line( target=target, previous_line=previous_line, next_line=next_line, original=target) except (SyntaxError, tokenize.TokenError): return [] if fixed: self.source[line_index] = fixed return [line_index + 1] else: return []
Example #18
Source File: autopep8.py From PyDev.Debugger with Eclipse Public License 1.0 | 6 votes |
def fix_w503(self, result): (line_index, _, target) = get_index_offset_contents(result, self.source) one_string_token = target.split()[0] try: ts = generate_tokens(one_string_token) except tokenize.TokenError: return if not _is_binary_operator(ts[0][0], one_string_token): return i = target.index(one_string_token) self.source[line_index] = '{0}{1}'.format( target[:i], target[i + len(one_string_token):]) nl = find_newline(self.source[line_index - 1:line_index]) before_line = self.source[line_index - 1] bl = before_line.index(nl) self.source[line_index - 1] = '{0} {1}{2}'.format( before_line[:bl], one_string_token, before_line[bl:])
Example #19
Source File: autopep8.py From PyDev.Debugger with Eclipse Public License 1.0 | 6 votes |
def refactor_with_2to3(source_text, fixer_names, filename=''): """Use lib2to3 to refactor the source. Return the refactored source code. """ from lib2to3.refactor import RefactoringTool fixers = ['lib2to3.fixes.fix_' + name for name in fixer_names] tool = RefactoringTool(fixer_names=fixers, explicit=fixers) from lib2to3.pgen2 import tokenize as lib2to3_tokenize try: # The name parameter is necessary particularly for the "import" fixer. return unicode(tool.refactor_string(source_text, name=filename)) except lib2to3_tokenize.TokenError: return source_text
Example #20
Source File: parser.py From coveragepy-bbmirror with Apache License 2.0 | 6 votes |
def parse_source(self): """Parse source text to find executable lines, excluded lines, etc. Sets the .excluded and .statements attributes, normalized to the first line of multi-line statements. """ try: self._raw_parse() except (tokenize.TokenError, IndentationError) as err: if hasattr(err, "lineno"): lineno = err.lineno # IndentationError else: lineno = err.args[1][0] # TokenError raise NotPython( u"Couldn't parse '%s' as Python source: '%s' at line %d" % ( self.filename, err.args[0], lineno ) ) self.excluded = self.first_lines(self.raw_excluded) ignore = self.excluded | self.raw_docstrings starts = self.raw_statements - ignore self.statements = self.first_lines(starts) - ignore
Example #21
Source File: evaluate.py From python_autocomplete with MIT License | 6 votes |
def __get_tokens(it): tokens: List[tokenize.TokenInfo] = [] try: for t in it: if t.type in tokenizer.SKIP_TOKENS: continue if t.type == tokenize.NEWLINE and t.string == '': continue if t.type == tokenize.DEDENT: continue if t.type == tokenize.ERRORTOKEN: continue tokens.append(t) except tokenize.TokenError as e: if not e.args[0].startswith('EOF in'): print(e) except IndentationError as e: print(e) return tokens
Example #22
Source File: check_whitespace.py From attention-lvcsr with MIT License | 6 votes |
def get_parse_error(code): """ Checks code for ambiguous tabs or other basic parsing issues. :param code: a string containing a file's worth of Python code :returns: a string containing a description of the first parse error encountered, or None if the code is ok """ # note that this uses non-public elements from stdlib's tabnanny, because tabnanny # is (very frustratingly) written only to be used as a script, but using it that way # in this context requires writing temporarily files, running subprocesses, blah blah blah code_buffer = StringIO(code) try: tabnanny.process_tokens(tokenize.generate_tokens(code_buffer.readline)) except tokenize.TokenError as err: return "Could not parse code: %s" % err except IndentationError as err: return "Indentation error: %s" % err except tabnanny.NannyNag as err: return "Ambiguous tab at line %d; line is '%s'." % (err.get_lineno(), err.get_line()) return None
Example #23
Source File: ScriptBinding.py From Fluid-Designer with GNU General Public License v3.0 | 6 votes |
def tabnanny(self, filename): # XXX: tabnanny should work on binary files as well with tokenize.open(filename) as f: try: tabnanny.process_tokens(tokenize.generate_tokens(f.readline)) except tokenize.TokenError as msg: msgtxt, (lineno, start) = msg.args self.editwin.gotoline(lineno) self.errorbox("Tabnanny Tokenizing Error", "Token Error: %s" % msgtxt) return False except tabnanny.NannyNag as nag: # The error messages from tabnanny are too confusing... self.editwin.gotoline(nag.get_lineno()) self.errorbox("Tab/space error", indent_message) return False return True
Example #24
Source File: tbtools.py From Building-Recommendation-Systems-with-Python with MIT License | 6 votes |
def get_annotated_lines(self): """Helper function that returns lines with extra information.""" lines = [Line(idx + 1, x) for idx, x in enumerate(self.sourcelines)] # find function definition and mark lines if hasattr(self.code, "co_firstlineno"): lineno = self.code.co_firstlineno - 1 while lineno > 0: if _funcdef_re.match(lines[lineno].code): break lineno -= 1 try: offset = len(inspect.getblock([x.code + "\n" for x in lines[lineno:]])) except TokenError: offset = 0 for line in lines[lineno : lineno + offset]: line.in_frame = True # mark current line try: lines[self.lineno - 1].current = True except IndexError: pass return lines
Example #25
Source File: tbtools.py From Building-Recommendation-Systems-with-Python with MIT License | 6 votes |
def get_annotated_lines(self): """Helper function that returns lines with extra information.""" lines = [Line(idx + 1, x) for idx, x in enumerate(self.sourcelines)] # find function definition and mark lines if hasattr(self.code, "co_firstlineno"): lineno = self.code.co_firstlineno - 1 while lineno > 0: if _funcdef_re.match(lines[lineno].code): break lineno -= 1 try: offset = len(inspect.getblock([x.code + "\n" for x in lines[lineno:]])) except TokenError: offset = 0 for line in lines[lineno : lineno + offset]: line.in_frame = True # mark current line try: lines[self.lineno - 1].current = True except IndexError: pass return lines
Example #26
Source File: split.py From flynt with MIT License | 6 votes |
def get_chunks(code) -> Generator[Chunk, None, None]: g = tokenize.tokenize(io.BytesIO(code.encode("utf-8")).readline) chunk = Chunk() try: for item in g: t = PyToken(item) reuse = chunk.append(t) if chunk.complete: yield chunk chunk = Chunk() if reuse: reuse = chunk.append(t) # assert not reuse if chunk.complete: yield chunk chunk = Chunk() yield chunk except tokenize.TokenError as e: if state.verbose: traceback.print_exc() print(e)
Example #27
Source File: tbtools.py From scylla with Apache License 2.0 | 6 votes |
def get_annotated_lines(self): """Helper function that returns lines with extra information.""" lines = [Line(idx + 1, x) for idx, x in enumerate(self.sourcelines)] # find function definition and mark lines if hasattr(self.code, "co_firstlineno"): lineno = self.code.co_firstlineno - 1 while lineno > 0: if _funcdef_re.match(lines[lineno].code): break lineno -= 1 try: offset = len(inspect.getblock([x.code + "\n" for x in lines[lineno:]])) except TokenError: offset = 0 for line in lines[lineno : lineno + offset]: line.in_frame = True # mark current line try: lines[self.lineno - 1].current = True except IndexError: pass return lines
Example #28
Source File: check_whitespace.py From D-VAE with MIT License | 6 votes |
def get_parse_error(code): """ Checks code for ambiguous tabs or other basic parsing issues. :param code: a string containing a file's worth of Python code :returns: a string containing a description of the first parse error encountered, or None if the code is ok """ # note that this uses non-public elements from stdlib's tabnanny, because tabnanny # is (very frustratingly) written only to be used as a script, but using it that way # in this context requires writing temporarily files, running subprocesses, blah blah blah code_buffer = StringIO(code) try: tabnanny.process_tokens(tokenize.generate_tokens(code_buffer.readline)) except tokenize.TokenError as err: return "Could not parse code: %s" % err except IndentationError as err: return "Indentation error: %s" % err except tabnanny.NannyNag as err: return "Ambiguous tab at line %d; line is '%s'." % (err.get_lineno(), err.get_line()) return None
Example #29
Source File: inspector.py From Computable with MIT License | 5 votes |
def inspect_traceback(tb): """Inspect a traceback and its frame, returning source for the expression where the exception was raised, with simple variable replacement performed and the line on which the exception was raised marked with '>>' """ log.debug('inspect traceback %s', tb) # we only want the innermost frame, where the exception was raised while tb.tb_next: tb = tb.tb_next frame = tb.tb_frame lines, exc_line = tbsource(tb) # figure out the set of lines to grab. inspect_lines, mark_line = find_inspectable_lines(lines, exc_line) src = StringIO(textwrap.dedent(''.join(inspect_lines))) exp = Expander(frame.f_locals, frame.f_globals) while inspect_lines: try: for tok in tokenize.generate_tokens(src.readline): exp(*tok) except tokenize.TokenError, e: # this can happen if our inspectable region happens to butt up # against the end of a construct like a docstring with the closing # """ on separate line log.debug("Tokenizer error: %s", e) inspect_lines.pop(0) mark_line -= 1 src = StringIO(textwrap.dedent(''.join(inspect_lines))) exp = Expander(frame.f_locals, frame.f_globals) continue break
Example #30
Source File: autopep8.py From python-netsurv with MIT License | 5 votes |
def _get_indentword(source): """Return indentation type.""" indent_word = ' ' # Default in case source has no indentation try: for t in generate_tokens(source): if t[0] == token.INDENT: indent_word = t[1] break except (SyntaxError, tokenize.TokenError): pass return indent_word