def visit(self, node): source_code = self.context.source_code source_file = self.context.source_file try: if source_code and hasattr(node, 'lineno'): self._lineno = node.lineno self._col_offset = node.col_offset if anno.hasanno(node, anno.Basic.SKIP_PROCESSING): return node return super(Base, self).visit(node) except (ValueError, AttributeError, KeyError, NotImplementedError, AssertionError) as e: msg = '%s: %s\nOccurred at node:\n%s' % ( e.__class__.__name__, str(e), pretty_printer.fmt(node, color=False)) if source_code: line = source_code.splitlines()[self._lineno - 1] else: line = '<no source available>' six.reraise( PyFlowParseError, PyFlowParseError( msg, (source_file, self._lineno, self._col_offset + 1, line)), sys.exc_info()[2])
def visit_FunctionDef(self, node): self.generic_visit(node) for dec in node.decorator_list: if isinstance(dec, gast.Call): dec = dec.func if not anno.hasanno(dec, 'live_val'): raise ValueError('Could not resolve decorator: %s' % pretty_printer.fmt(dec)) dec_value = anno.getanno(dec, 'live_val') if dec_value in self.remove_decorators: continue raise ValueError('Dont know how to convert decorators for now.') node.decorator_list = [] return node
def visit_FunctionDef(self, node): self.generic_visit(node) for dec in node.decorator_list: if isinstance(dec, gast.Call): dec = dec.func if not anno.hasanno(dec, 'live_val'): raise ValueError( 'Could not resolve decorator: %s' % pretty_printer.fmt(dec)) dec_value = anno.getanno(dec, 'live_val') if dec_value in self.remove_decorators: continue raise ValueError('Dont know how to convert decorators for now.') node.decorator_list = [] return node
def visit(self, node): try: if self._source and hasattr(node, 'lineno'): self._lineno = node.lineno self._col_offset = node.col_offset return super(Base, self).visit(node) except ValueError as e: msg = '%s\nOccurred at node:\n%s' % (str(e), pretty_printer.fmt(node)) if self._source: line = self._source.splitlines()[self._lineno - 1] else: line = '<no source available>' raise PyFlowParseError( msg, (self._file, self._lineno, self._col_offset + 1, line))
def visit_FunctionDef(self, node): self.generic_visit(node) kept_decorators = [] for dec in node.decorator_list: if isinstance(dec, gast.Call): dec_func = dec.func else: dec_func = dec if not anno.hasanno(dec_func, 'live_val'): raise ValueError( 'Could not resolve decorator: %s' % pretty_printer.fmt(dec_func)) dec_value = anno.getanno(dec_func, 'live_val') if dec_value not in self.remove_decorators: kept_decorators.append(dec) node.decorator_list = kept_decorators return node
def visit_FunctionDef(self, node): self.generic_visit(node) kept_decorators = [] for dec in node.decorator_list: if isinstance(dec, gast.Call): dec_func = dec.func else: dec_func = dec if not anno.hasanno(dec_func, 'live_val'): raise ValueError('Could not resolve decorator: %s' % pretty_printer.fmt(dec_func)) dec_value = anno.getanno(dec_func, 'live_val') if dec_value not in self.remove_decorators: kept_decorators.append(dec) node.decorator_list = kept_decorators return node
def visit(self, node): try: source_code = self.context.source_code source_file = self.context.source_file if source_code and hasattr(node, 'lineno'): self._lineno = node.lineno self._col_offset = node.col_offset return super(Base, self).visit(node) except ValueError as e: msg = '%s\nOccurred at node:\n%s' % (str(e), pretty_printer.fmt(node)) if source_code: line = self._source.splitlines()[self._lineno - 1] else: line = '<no source available>' raise PyFlowParseError( msg, (source_file, self._lineno, self._col_offset + 1, line))
def test_format(self): node = ast.FunctionDef(name='f', args=ast.arguments( args=[ast.Name(id='a', ctx=ast.Param())], vararg=None, kwarg=None, defaults=[]), body=[ ast.Return( ast.BinOp(op=ast.Add(), left=ast.Name(id='a', ctx=ast.Load()), right=ast.Num(1))) ], decorator_list=[], returns=None) # Just checking for functionality, the color control characters make it # difficult to inspect the result. self.assertIsNotNone(pretty_printer.fmt(node))
def visit_FunctionDef(self, node): self.generic_visit(node) kept_decorators = [] for dec in node.decorator_list: if isinstance(dec, gast.Call): dec_func = dec.func else: dec_func = dec # Special cases. # TODO(mdan): Is there any way we can treat these more generically? # We may want to forego using decorators altogether if we can't # properly support them. if isinstance(dec_func, gast.Name) and dec_func.id in ('classmethod', ): # Assumption: decorators are only visible in the AST when converting # a function inline (via another decorator). # In that case, the converted function is no longer part of the # original object that it was declared into. # This is currently verified by tests. continue if not anno.hasanno(dec_func, 'live_val'): raise ValueError('Could not resolve decorator: %s' % pretty_printer.fmt(dec_func)) dec_value = anno.getanno(dec_func, 'live_val') if dec_value not in self.remove_decorators: kept_decorators.append((dec, dec_value)) for _, dec_value in kept_decorators: if dec_value.__module__ == '__main__': raise ValueError( 'decorator "%s" was not allowed because it is declared ' 'in the module "%s". To fix this, declare it in a separate ' 'module that we can import it from.' % (dec_value, dec_value.__module__)) else: self.additional_dependencies.add(dec_value) node.decorator_list = [dec for dec, _ in kept_decorators] return node
def visit(self, node): try: source_code = self.context.source_code source_file = self.context.source_file if source_code and hasattr(node, 'lineno'): self._lineno = node.lineno self._col_offset = node.col_offset return super(Base, self).visit(node) except (ValueError, AttributeError, NotImplementedError) as e: msg = '%s: %s\nOccurred at node:\n%s' % (e.__class__.__name__, str(e), pretty_printer.fmt(node)) if source_code: line = source_code.splitlines()[self._lineno - 1] else: line = '<no source available>' six.reraise(PyFlowParseError, PyFlowParseError( msg, (source_file, self._lineno, self._col_offset + 1, line)), sys.exc_info()[2])
def test_format(self): node = ast.FunctionDef( name='f', args=ast.arguments( args=[ast.Name(id='a', ctx=ast.Param())], vararg=None, kwarg=None, defaults=[]), body=[ ast.Return( ast.BinOp( op=ast.Add(), left=ast.Name(id='a', ctx=ast.Load()), right=ast.Num(1))) ], decorator_list=[], returns=None) # Just checking for functionality, the color control characters make it # difficult to inspect the result. self.assertIsNotNone(pretty_printer.fmt(node))
def visit_FunctionDef(self, node): self.generic_visit(node) kept_decorators = [] for dec in node.decorator_list: if isinstance(dec, gast.Call): dec_func = dec.func else: dec_func = dec # Special cases. # TODO(mdan): Is there any way we can treat these more generically? # We may want to forego using decorators altogether if we can't # properly support them. if isinstance(dec_func, gast.Name) and dec_func.id in ('classmethod',): # Assumption: decorators are only visible in the AST when converting # a function inline (via another decorator). # In that case, the converted function is no longer part of the # original object that it was declared into. # This is currently verified by tests. continue if not anno.hasanno(dec_func, 'live_val'): raise ValueError( 'Could not resolve decorator: %s' % pretty_printer.fmt(dec_func)) dec_value = anno.getanno(dec_func, 'live_val') if dec_value not in self.remove_decorators: kept_decorators.append((dec, dec_value)) for _, dec_value in kept_decorators: if dec_value.__module__ == '__main__': raise ValueError( 'decorator "%s" was not allowed because it is declared ' 'in the module "%s". To fix this, declare it in a separate ' 'module that we can import it from.' % (dec_value, dec_value.__module__)) else: self.additional_dependencies.add(dec_value) node.decorator_list = [dec for dec, _ in kept_decorators] return node
def compiled(self, node, *symbols): source = None self.dynamic_calls = [] def converted_call(*args): """Mock version of api.converted_call.""" self.dynamic_calls.append(args) return 7 try: result, source = compiler.ast_to_object(node) result.tf = self.make_fake_mod('fake_tf', *symbols) result.py2tf_utils = utils result.py2tf_api = self.make_fake_mod('fake_api', converted_call) yield result except Exception: # pylint:disable=broad-except if source is None: print('Offending AST:\n%s' % pretty_printer.fmt(node, color=False)) else: print('Offending compiled code:\n%s' % source) raise
def visit(self, node): source_code = self.context.source_code source_file = self.context.source_file try: if source_code and hasattr(node, 'lineno'): self._lineno = node.lineno self._col_offset = node.col_offset if anno.hasanno(node, anno.Basic.SKIP_PROCESSING): return node return super(Base, self).visit(node) except (ValueError, AttributeError, KeyError, NotImplementedError, AssertionError) as e: msg = '%s: %s\nOffending source:\n%s\n\nOccurred at node:\n%s' % ( e.__class__.__name__, str(e), try_ast_to_source(node), pretty_printer.fmt(node, color=False)) if source_code: line = source_code.splitlines()[self._lineno - 1] else: line = '<no source available>' six.reraise(PyFlowParseError, PyFlowParseError( msg, (source_file, self._lineno, self._col_offset + 1, line)), sys.exc_info()[2])
def debug_print(self, node): """Helper method useful for debugging.""" if __debug__: print(pretty_printer.fmt(node)) return node