<s> """<STR_LIT>""" <EOL> from __future__ import division <EOL> import sys <EOL> import traceback <EOL> from vistrails . core . data_structures . stack import Stack <EOL> import unittest <EOL> import tempfile <EOL> import os <EOL> _output_file = sys . stderr <EOL> __current_method_name = Stack ( ) <EOL> def _indent ( ) : <EOL> _output_file . write ( '<STR_LIT:U+0020>' * ( len ( __current_method_name ) - <NUM_LIT:1> ) ) <EOL> def trace_method_options ( method , <EOL> with_args = False , <EOL> with_kwargs = False , <EOL> with_return = False ) : <EOL> """<STR_LIT>""" <EOL> def decorated ( self , * args , ** kwargs ) : <EOL> __current_method_name . push ( [ method . __name__ , <NUM_LIT:0> ] ) <EOL> try : <EOL> _indent ( ) <EOL> _output_file . write ( method . __name__ + "<STR_LIT>" ) <EOL> if with_args : <EOL> _output_file . write ( "<STR_LIT>" ) <EOL> _output_file . write ( str ( [ str ( arg ) for arg in args ] ) ) <EOL> _output_file . write ( "<STR_LIT:)>" ) <EOL> if with_kwargs : <EOL> _output_file . write ( "<STR_LIT>" ) <EOL> kwarglist = [ ( k , str ( v ) ) for ( k , v ) in kwargs . iteritems ( ) ] <EOL> kwarglist . sort ( ) <EOL> _output_file . write ( str ( kwarglist ) ) <EOL> _output_file . write ( "<STR_LIT:)>" ) <EOL> _output_file . write ( '<STR_LIT:\n>' ) <EOL> result = method ( self , * args , ** kwargs ) <EOL> _indent ( ) <EOL> _output_file . write ( method . __name__ + "<STR_LIT>" ) <EOL> if with_return : <EOL> _output_file . write ( "<STR_LIT>" % str ( result ) ) <EOL> _output_file . write ( '<STR_LIT:\n>' ) <EOL> finally : <EOL> __current_method_name . pop ( ) <EOL> return result <EOL> return decorated <EOL> def trace_method ( method ) : <EOL> return trace_method_options ( method ) <EOL> def trace_method_args ( method ) : <EOL> return trace_method_options ( method , with_args = True ) <EOL> def bump_trace ( ) : <EOL> __current_method_name . top ( ) [ <NUM_LIT:1> ] += <NUM_LIT:1> <EOL> _indent ( ) <EOL> _output_file . write ( '<STR_LIT>' % tuple ( __current_method_name . top ( ) ) ) <EOL> def report_stack ( method ) : <EOL> def decorated ( self , * args , ** kwargs ) : <EOL> print "<STR_LIT:->" * <NUM_LIT> <EOL> try : <EOL> print "<STR_LIT>" + method . im_class . __name__ + '<STR_LIT:.>' + method . __name__ <EOL> except AttributeError : <EOL> pass <EOL> try : <EOL> print "<STR_LIT>" + method . func_name <EOL> except AttributeError : <EOL> pass <EOL> traceback . print_stack ( ) <EOL> print "<STR_LIT:->" * <NUM_LIT> <EOL> return method ( self , * args , ** kwargs ) <EOL> return decorated <EOL> @ trace_method <EOL> def test_fun ( p1 ) : <EOL> return p1 + <NUM_LIT:5> <EOL> @ trace_method <EOL> def test_fun_2 ( p1 ) : <EOL> bump_trace ( ) <EOL> result = test_fun ( p1 ) + <NUM_LIT:3> <EOL> bump_trace ( ) <EOL> return result <EOL> class TestTraceMethod ( unittest . TestCase ) : <EOL> def test_trace_1 ( self ) : <EOL> global _output_file <EOL> ( fd , name ) = tempfile . mkstemp ( ) <EOL> os . close ( fd ) <EOL> try : <EOL> _output_file = open ( name , '<STR_LIT:w>' ) <EOL> x = test_fun ( <NUM_LIT:10> ) <EOL> self . assertEquals ( x , <NUM_LIT:15> ) <EOL> _output_file . close ( ) <EOL> _output_file = sys . stderr <EOL> output = "<STR_LIT>" . join ( open ( name , '<STR_LIT:r>' ) . readlines ( ) ) <EOL> self . assertEquals ( output , <EOL> '<STR_LIT>' + <EOL> '<STR_LIT>' ) <EOL> finally : <EOL> os . unlink ( name ) <EOL> def test_trace_2 ( self ) : <EOL> global _output_file <EOL> ( fd , name ) = tempfile . mkstemp ( ) <EOL> os . close ( fd ) <EOL> _output_file = open ( name , '<STR_LIT:w>' ) <EOL> x = test_fun_2 ( <NUM_LIT:10> ) <EOL> self . assertEquals ( x , <NUM_LIT> ) <EOL> _output_file . close ( ) <EOL> _output_file = sys . stderr <EOL> output = "<STR_LIT>" . join ( open ( name , '<STR_LIT:r>' ) . readlines ( ) ) <EOL> self . assertEquals ( output , <EOL> '<STR_LIT>' + <EOL> '<STR_LIT>' + <EOL> '<STR_LIT>' + <EOL> '<STR_LIT>' + <EOL> '<STR_LIT>' + <EOL> '<STR_LIT>' ) <EOL> os . unlink ( name ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> unittest . main ( ) </s>
