Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _fix_py3_plus(contents_text): # type: (str) -> str
try:
ast_obj = ast_parse(contents_text)
except SyntaxError:
return contents_text
visitor = FindPy3Plus()
visitor.visit(ast_obj)
if not any((
visitor.bases_to_remove,
visitor.encode_calls,
visitor.if_py2_blocks,
visitor.if_py3_blocks,
visitor.native_literals,
visitor.io_open_calls,
visitor.os_error_alias_calls,
visitor.os_error_alias_simple,
visitor.os_error_alias_excepts,
visitor.six_add_metaclass,
visitor.six_b,
visitor.six_calls,
visitor.six_iter,
for base in node.bases:
if isinstance(base, ast.Name) and base.id == 'object':
self.bases_to_remove.add(_ast_to_offset(base))
elif self._is_six(base, ('Iterator',)):
self.bases_to_remove.add(_ast_to_offset(base))
if (
len(node.bases) == 1 and
isinstance(node.bases[0], ast.Call) and
self._is_six(node.bases[0].func, ('with_metaclass',)) and
not _starargs(node.bases[0])
):
self.six_with_metaclass.add(_ast_to_offset(node.bases[0]))
self._class_info_stack.append(FindPy3Plus.ClassInfo(node.name))
self.generic_visit(node)
self._class_info_stack.pop()
def generic_visit(self, node): # type: (ast.AST) -> None
self._previous_node = node
super(FindPy3Plus, self).generic_visit(node)
def _fix_percent_format(contents_text): # type: (str) -> str
try:
ast_obj = ast_parse(contents_text)
except SyntaxError:
return contents_text
visitor = FindPercentFormats()
visitor.visit(ast_obj)
if not visitor.found:
return contents_text
try:
tokens = src_to_tokens(contents_text)
except tokenize.TokenError: # pragma: no cover (bpo-2180)
return contents_text
for i, token in reversed_enumerate(tokens):
node = visitor.found.get(token.offset)
if node is None:
continue
# no .format() equivalent for bytestrings in py3
def _fix_fstrings(contents_text): # type: (str) -> str
try:
ast_obj = ast_parse(contents_text)
except SyntaxError:
return contents_text
visitor = FindSimpleFormats()
visitor.visit(ast_obj)
if not visitor.found:
return contents_text
try:
tokens = src_to_tokens(contents_text)
except tokenize.TokenError: # pragma: no cover (bpo-2180)
return contents_text
for i, token in reversed_enumerate(tokens):
node = visitor.found.get(token.offset)
if node is None:
continue
if _is_bytestring(token.src): # pragma: no cover (py2-only)
continue
def _fix_py2_compatible(contents_text): # type: (str) -> str
try:
ast_obj = ast_parse(contents_text)
except SyntaxError:
return contents_text
visitor = Py2CompatibleVisitor()
visitor.visit(ast_obj)
if not any((
visitor.dicts,
visitor.sets,
visitor.set_empty_literals,
visitor.is_literal,
)):
return contents_text
try:
tokens = src_to_tokens(contents_text)
except tokenize.TokenError: # pragma: no cover (bpo-2180)
return contents_text
for i, token in reversed_enumerate(tokens):
if token.offset in visitor.dicts:
_process_dict_comp(tokens, i, visitor.dicts[token.offset])
def _find_if_else_block(tokens, i):
# type: (List[Token], int) -> Tuple[Block, Block]
if_block = Block.find(tokens, i)
i = if_block.end
while tokens[i].src != 'else':
i += 1
else_block = Block.find(tokens, i, trim_end=True)
return if_block, else_block
def _replace_yield(tokens, i): # type: (List[Token], int) -> None
in_token = _find_token(tokens, i, 'in')
colon = _find_block_start(tokens, i)
block = Block.find(tokens, i, trim_end=True)
container = tokens_to_src(tokens[in_token + 1:colon]).strip()
tokens[i:block.end] = [Token('CODE', 'yield from {}\n'.format(container))]
def _find_if_else_block(tokens, i):
# type: (List[Token], int) -> Tuple[Block, Block]
if_block = Block.find(tokens, i)
i = if_block.end
while tokens[i].src != 'else':
i += 1
else_block = Block.find(tokens, i, trim_end=True)
return if_block, else_block