def scan(self, code, filename=""): self.code = code.splitlines() self.noqa_lines = noqa.parse_noqa(self.code) self.filename = filename def handle_syntax_error(e): text = f' at "{e.text.strip()}"' if e.text else "" print( f"{utils.format_path(filename)}:{e.lineno}: {e.msg}{text}", file=sys.stderr, ) self.found_dead_code_or_error = True try: node = ( ast.parse(code, filename=self.filename, type_comments=True) if sys.version_info >= (3, 8) # type_comments requires 3.8+ else ast.parse(code, filename=self.filename)) except SyntaxError as err: handle_syntax_error(err) except ValueError as err: # ValueError is raised if source contains null bytes. print( f'{utils.format_path(filename)}: invalid source code "{err}"', file=sys.stderr, ) self.found_dead_code_or_error = True else: # When parsing type comments, visiting can throw SyntaxError. try: self.visit(node) except SyntaxError as err: handle_syntax_error(err)
def scan(self, code, filename=""): code = utils.sanitize_code(code) self.code = code.splitlines() self.noqa_lines = noqa.parse_noqa(self.code) self.filename = filename try: node = ast.parse(code, filename=self.filename) except SyntaxError as err: text = ' at "{}"'.format(err.text.strip()) if err.text else "" print( "{}:{:d}: {}{}".format( utils.format_path(filename), err.lineno, err.msg, text ), file=sys.stderr, ) self.found_dead_code_or_error = True except (TypeError, ValueError) as err: # Python < 3.5 raises TypeError and Python >= 3.5 raises # ValueError if source contains null bytes. print( '{}: invalid source code "{}"'.format( utils.format_path(filename), err ), file=sys.stderr, ) self.found_dead_code_or_error = True else: self.visit(node)
def scan(self, code, filename=''): self.code = code.splitlines() self.noqa_lines = noqa.parse_noqa(self.code) self.filename = filename try: node = parse_python_file(self.filename) except (SyntaxError, TypeError, ValueError) as err: self._tidypy_issues.append(ParseIssue(err, self.filename)) self.found_dead_code_or_error = True else: self.visit(node)