<s> from test import test_support <EOL> import types <EOL> import unittest <EOL> class FuncAttrsTest ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> class F : <EOL> def a ( self ) : <EOL> pass <EOL> def b ( ) : <EOL> return <NUM_LIT:3> <EOL> self . f = F <EOL> self . fi = F ( ) <EOL> self . b = b <EOL> def cannot_set_attr ( self , obj , name , value , exceptions ) : <EOL> try : <EOL> setattr ( obj , name , value ) <EOL> except exceptions : <EOL> pass <EOL> else : <EOL> self . fail ( "<STR_LIT>" % ( name , value ) ) <EOL> try : <EOL> delattr ( obj , name ) <EOL> except exceptions : <EOL> pass <EOL> else : <EOL> self . fail ( "<STR_LIT>" % name ) <EOL> class FunctionPropertiesTest ( FuncAttrsTest ) : <EOL> def test_module ( self ) : <EOL> self . assertEqual ( self . b . __module__ , __name__ ) <EOL> def test_dir_includes_correct_attrs ( self ) : <EOL> self . b . known_attr = <NUM_LIT:7> <EOL> self . assertIn ( '<STR_LIT>' , dir ( self . b ) , <EOL> "<STR_LIT>" ) <EOL> self . f . a . im_func . known_attr = <NUM_LIT:7> <EOL> self . assertIn ( '<STR_LIT>' , dir ( self . f . a ) , <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> self . assertIn ( '<STR_LIT>' , dir ( self . fi . a ) , <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> def test_duplicate_function_equality ( self ) : <EOL> def duplicate ( ) : <EOL> '<STR_LIT>' <EOL> return <NUM_LIT:3> <EOL> self . assertNotEqual ( self . b , duplicate ) <EOL> @ unittest . skipIf ( test_support . is_jython , "<STR_LIT>" ) <EOL> def test_copying_func_code ( self ) : <EOL> def test ( ) : pass <EOL> self . assertEqual ( test ( ) , None ) <EOL> test . func_code = self . b . func_code <EOL> self . assertEqual ( test ( ) , <NUM_LIT:3> ) <EOL> @ unittest . skipIf ( test_support . is_jython , "<STR_LIT>" ) <EOL> def test_func_globals ( self ) : <EOL> self . assertIs ( self . b . func_globals , globals ( ) ) <EOL> self . cannot_set_attr ( self . b , '<STR_LIT>' , <NUM_LIT:2> , TypeError ) <EOL> @ unittest . skipIf ( test_support . is_jython , "<STR_LIT>" ) <EOL> def test_func_closure ( self ) : <EOL> a = <NUM_LIT:12> <EOL> def f ( ) : print a <EOL> c = f . func_closure <EOL> self . assertIsInstance ( c , tuple ) <EOL> self . assertEqual ( len ( c ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( c [ <NUM_LIT:0> ] . __class__ . __name__ , "<STR_LIT>" ) <EOL> self . cannot_set_attr ( f , "<STR_LIT>" , c , TypeError ) <EOL> def test_empty_cell ( self ) : <EOL> def f ( ) : print a <EOL> try : <EOL> f . func_closure [ <NUM_LIT:0> ] . cell_contents <EOL> except ValueError : <EOL> pass <EOL> else : <EOL> self . fail ( "<STR_LIT>" ) <EOL> a = <NUM_LIT:12> <EOL> @ unittest . skipIf ( test_support . is_jython , "<STR_LIT>" ) <EOL> def test_func_name ( self ) : <EOL> self . assertEqual ( self . b . __name__ , '<STR_LIT:b>' ) <EOL> self . assertEqual ( self . b . func_name , '<STR_LIT:b>' ) <EOL> self . b . __name__ = '<STR_LIT:c>' <EOL> self . assertEqual ( self . b . __name__ , '<STR_LIT:c>' ) <EOL> self . assertEqual ( self . b . func_name , '<STR_LIT:c>' ) <EOL> self . b . func_name = '<STR_LIT:d>' <EOL> self . assertEqual ( self . b . __name__ , '<STR_LIT:d>' ) <EOL> self . assertEqual ( self . b . func_name , '<STR_LIT:d>' ) <EOL> self . cannot_set_attr ( self . b , '<STR_LIT>' , <NUM_LIT:7> , TypeError ) <EOL> self . cannot_set_attr ( self . b , '<STR_LIT>' , <NUM_LIT:7> , TypeError ) <EOL> s = """<STR_LIT>""" <EOL> exec s in { '<STR_LIT>' : { } } <EOL> self . assertEqual ( self . f . a . __name__ , '<STR_LIT:a>' ) <EOL> self . assertEqual ( self . fi . a . __name__ , '<STR_LIT:a>' ) <EOL> self . cannot_set_attr ( self . f . a , "<STR_LIT>" , '<STR_LIT:a>' , AttributeError ) <EOL> self . cannot_set_attr ( self . fi . a , "<STR_LIT>" , '<STR_LIT:a>' , AttributeError ) <EOL> @ unittest . skipIf ( test_support . is_jython , "<STR_LIT>" ) <EOL> def test_func_code ( self ) : <EOL> num_one , num_two = <NUM_LIT:7> , <NUM_LIT:8> <EOL> def a ( ) : pass <EOL> def b ( ) : return <NUM_LIT:12> <EOL> def c ( ) : return num_one <EOL> def d ( ) : return num_two <EOL> def e ( ) : return num_one , num_two <EOL> for func in [ a , b , c , d , e ] : <EOL> self . assertEqual ( type ( func . func_code ) , types . CodeType ) <EOL> self . assertEqual ( c ( ) , <NUM_LIT:7> ) <EOL> self . assertEqual ( d ( ) , <NUM_LIT:8> ) <EOL> d . func_code = c . func_code <EOL> self . assertEqual ( c . func_code , d . func_code ) <EOL> self . assertEqual ( c ( ) , <NUM_LIT:7> ) <EOL> try : <EOL> b . func_code = c . func_code <EOL> except ValueError : <EOL> pass <EOL> else : <EOL> self . fail ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> try : <EOL> e . func_code = d . func_code <EOL> except ValueError : <EOL> pass <EOL> else : <EOL> self . fail ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> def test_blank_func_defaults ( self ) : <EOL> self . assertEqual ( self . b . func_defaults , None ) <EOL> del self . b . func_defaults <EOL> self . assertEqual ( self . b . func_defaults , None ) <EOL> def test_func_default_args ( self ) : <EOL> def first_func ( a , b ) : <EOL> return a + b <EOL> def second_func ( a = <NUM_LIT:1> , b = <NUM_LIT:2> ) : <EOL> return a + b <EOL> self . assertEqual ( first_func . func_defaults , None ) <EOL> self . assertEqual ( second_func . func_defaults , ( <NUM_LIT:1> , <NUM_LIT:2> ) ) <EOL> first_func . func_defaults = ( <NUM_LIT:1> , <NUM_LIT:2> ) <EOL> self . assertEqual ( first_func . func_defaults , ( <NUM_LIT:1> , <NUM_LIT:2> ) ) <EOL> self . assertEqual ( first_func ( ) , <NUM_LIT:3> ) <EOL> self . assertEqual ( first_func ( <NUM_LIT:3> ) , <NUM_LIT:5> ) <EOL> self . assertEqual ( first_func ( <NUM_LIT:3> , <NUM_LIT:5> ) , <NUM_LIT:8> ) <EOL> del second_func . func_defaults <EOL> self . assertEqual ( second_func . func_defaults , None ) <EOL> try : <EOL> second_func ( ) <EOL> except TypeError : <EOL> pass <EOL> else : <EOL> self . fail ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> class InstancemethodAttrTest ( FuncAttrsTest ) : <EOL> def test_im_class ( self ) : <EOL> self . assertEqual ( self . f . a . im_class , self . f ) <EOL> self . assertEqual ( self . fi . a . im_class , self . f ) <EOL> self . cannot_set_attr ( self . f . a , "<STR_LIT>" , self . f , TypeError ) <EOL> self . cannot_set_attr ( self . fi . a , "<STR_LIT>" , self . f , TypeError ) <EOL> def test_im_func ( self ) : <EOL> self . f . b = self . b <EOL> self . assertEqual ( self . f . b . im_func , self . b ) <EOL> self . assertEqual ( self . fi . b . im_func , self . b ) <EOL> self . cannot_set_attr ( self . f . b , "<STR_LIT>" , self . b , TypeError ) <EOL> self . cannot_set_attr ( self . fi . b , "<STR_LIT>" , self . b , TypeError ) <EOL> def test_im_self ( self ) : <EOL> self . assertEqual ( self . f . a . im_self , None ) <EOL> self . assertEqual ( self . fi . a . im_self , self . fi ) <EOL> self . cannot_set_attr ( self . f . a , "<STR_LIT>" , None , TypeError ) <EOL> self . cannot_set_attr ( self . fi . a , "<STR_LIT>" , self . fi , TypeError ) <EOL> def test_im_func_non_method ( self ) : <EOL> self . f . id = types . MethodType ( id , None , self . f ) <EOL> self . assertEqual ( self . fi . id ( ) , id ( self . fi ) ) <EOL> self . assertNotEqual ( self . fi . id ( ) , id ( self . f ) ) <EOL> try : <EOL> self . f . id . unknown_attr <EOL> except AttributeError : <EOL> pass <EOL> else : <EOL> self . fail ( "<STR_LIT>" ) <EOL> self . cannot_set_attr ( self . f . id , '<STR_LIT>' , <NUM_LIT:2> , AttributeError ) <EOL> self . cannot_set_attr ( self . fi . id , '<STR_LIT>' , <NUM_LIT:2> , AttributeError ) <EOL> def test_implicit_method_properties ( self ) : <EOL> self . f . a . im_func . known_attr = <NUM_LIT:7> <EOL> self . assertEqual ( self . f . a . known_attr , <NUM_LIT:7> ) <EOL> self . assertEqual ( self . fi . a . known_attr , <NUM_LIT:7> ) <EOL> class ArbitraryFunctionAttrTest ( FuncAttrsTest ) : <EOL> def test_set_attr ( self ) : <EOL> self . b . known_attr = <NUM_LIT:7> <EOL> self . assertEqual ( self . b . known_attr , <NUM_LIT:7> ) <EOL> for func in [ self . f . a , self . fi . a ] : <EOL> try : <EOL> func . known_attr = <NUM_LIT:7> <EOL> except AttributeError : <EOL> pass <EOL> else : <EOL> self . fail ( "<STR_LIT>" ) <EOL> def test_delete_unknown_attr ( self ) : <EOL> try : <EOL> del self . b . unknown_attr <EOL> except AttributeError : <EOL> pass <EOL> else : <EOL> self . fail ( "<STR_LIT>" ) <EOL> def test_setting_attrs_duplicates ( self ) : <EOL> try : <EOL> self . f . a . klass = self . f <EOL> except AttributeError : <EOL> pass <EOL> else : <EOL> self . fail ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> self . f . a . im_func . klass = self . f <EOL> for method in [ self . f . a , self . fi . a , self . fi . a . im_func ] : <EOL> self . assertEqual ( method . klass , self . f ) <EOL> def test_unset_attr ( self ) : <EOL> for func in [ self . b , self . f . a , self . fi . a ] : <EOL> try : <EOL> func . non_existent_attr <EOL> except AttributeError : <EOL> pass <EOL> else : <EOL> self . fail ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> class FunctionDictsTest ( FuncAttrsTest ) : <EOL> def test_setting_dict_to_invalid ( self ) : <EOL> self . cannot_set_attr ( self . b , '<STR_LIT>' , None , TypeError ) <EOL> self . cannot_set_attr ( self . b , '<STR_LIT>' , None , TypeError ) <EOL> from UserDict import UserDict <EOL> d = UserDict ( { '<STR_LIT>' : <NUM_LIT:7> } ) <EOL> self . cannot_set_attr ( self . f . a . im_func , '<STR_LIT>' , d , TypeError ) <EOL> self . cannot_set_attr ( self . fi . a . im_func , '<STR_LIT>' , d , TypeError ) <EOL> def test_setting_dict_to_valid ( self ) : <EOL> d = { '<STR_LIT>' : <NUM_LIT:7> } <EOL> self . b . __dict__ = d <EOL> self . f . a . im_func . __dict__ = d <EOL> self . assertIs ( d , self . b . __dict__ ) <EOL> self . assertIs ( d , self . b . func_dict ) <EOL> self . assertIs ( d , self . f . a . im_func . __dict__ ) <EOL> self . assertIs ( d , self . f . a . __dict__ ) <EOL> self . assertIs ( d , self . fi . a . im_func . __dict__ ) <EOL> self . assertIs ( d , self . fi . a . __dict__ ) <EOL> self . assertEqual ( self . b . known_attr , <NUM_LIT:7> ) <EOL> self . assertEqual ( self . b . __dict__ [ '<STR_LIT>' ] , <NUM_LIT:7> ) <EOL> self . assertEqual ( self . b . func_dict [ '<STR_LIT>' ] , <NUM_LIT:7> ) <EOL> self . assertEqual ( self . f . a . im_func . known_attr , <NUM_LIT:7> ) <EOL> self . assertEqual ( self . f . a . known_attr , <NUM_LIT:7> ) <EOL> self . assertEqual ( self . fi . a . im_func . known_attr , <NUM_LIT:7> ) <EOL> self . assertEqual ( self . fi . a . known_attr , <NUM_LIT:7> ) <EOL> def test_delete_func_dict ( self ) : <EOL> try : <EOL> del self . b . __dict__ <EOL> except TypeError : <EOL> pass <EOL> else : <EOL> self . fail ( "<STR_LIT>" ) <EOL> try : <EOL> del self . b . func_dict <EOL> except TypeError : <EOL> pass <EOL> else : <EOL> self . fail ( "<STR_LIT>" ) <EOL> def test_unassigned_dict ( self ) : <EOL> self . assertEqual ( self . b . __dict__ , { } ) <EOL> def test_func_as_dict_key ( self ) : <EOL> value = "<STR_LIT>" <EOL> d = { } <EOL> d [ self . b ] = value <EOL> self . assertEqual ( d [ self . b ] , value ) <EOL> class FunctionDocstringTest ( FuncAttrsTest ) : <EOL> @ unittest . skipIf ( test_support . is_jython , "<STR_LIT>" ) <EOL> def test_set_docstring_attr ( self ) : <EOL> self . assertEqual ( self . b . __doc__ , None ) <EOL> self . assertEqual ( self . b . func_doc , None ) <EOL> docstr = "<STR_LIT>" <EOL> self . b . __doc__ = self . f . a . im_func . __doc__ = docstr <EOL> self . assertEqual ( self . b . __doc__ , docstr ) <EOL> self . assertEqual ( self . b . func_doc , docstr ) <EOL> self . assertEqual ( self . f . a . __doc__ , docstr ) <EOL> self . assertEqual ( self . fi . a . __doc__ , docstr ) <EOL> self . cannot_set_attr ( self . f . a , "<STR_LIT>" , docstr , AttributeError ) <EOL> self . cannot_set_attr ( self . fi . a , "<STR_LIT>" , docstr , AttributeError ) <EOL> def test_delete_docstring ( self ) : <EOL> self . b . __doc__ = "<STR_LIT>" <EOL> del self . b . __doc__ <EOL> self . assertEqual ( self . b . __doc__ , None ) <EOL> self . assertEqual ( self . b . func_doc , None ) <EOL> self . b . func_doc = "<STR_LIT>" <EOL> del self . b . func_doc <EOL> self . assertEqual ( self . b . __doc__ , None ) <EOL> self . assertEqual ( self . b . func_doc , None ) <EOL> class StaticMethodAttrsTest ( unittest . TestCase ) : <EOL> @ unittest . skipIf ( test_support . is_jython , "<STR_LIT>" ) <EOL> def test_func_attribute ( self ) : <EOL> def f ( ) : <EOL> pass <EOL> c = classmethod ( f ) <EOL> self . assertTrue ( c . __func__ is f ) <EOL> s = staticmethod ( f ) <EOL> self . assertTrue ( s . __func__ is f ) <EOL> def test_main ( ) : <EOL> test_support . run_unittest ( FunctionPropertiesTest , InstancemethodAttrTest , <EOL> ArbitraryFunctionAttrTest , FunctionDictsTest , <EOL> FunctionDocstringTest , <EOL> StaticMethodAttrsTest ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> test_main ( ) </s>
