<s> import ast <EOL> import numbers <EOL> from bpython . simpleeval import ( simple_eval , <EOL> evaluate_current_expression , <EOL> EvaluationError , <EOL> safe_get_attribute , <EOL> safe_get_attribute_new_style ) <EOL> from bpython . test import unittest <EOL> from bpython . _py3compat import py3 <EOL> class TestSimpleEval ( unittest . TestCase ) : <EOL> def assertMatchesStdlib ( self , expr ) : <EOL> self . assertEqual ( ast . literal_eval ( expr ) , simple_eval ( expr ) ) <EOL> def test_matches_stdlib ( self ) : <EOL> """<STR_LIT>""" <EOL> self . assertMatchesStdlib ( "<STR_LIT>" ) <EOL> self . assertMatchesStdlib ( "<STR_LIT>" ) <EOL> def test_indexing ( self ) : <EOL> """<STR_LIT>""" <EOL> self . assertEqual ( simple_eval ( '<STR_LIT>' ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( simple_eval ( '<STR_LIT:a>' , { '<STR_LIT:a>' : <NUM_LIT:1> } ) , <NUM_LIT:1> ) <EOL> def test_name_lookup ( self ) : <EOL> """<STR_LIT>""" <EOL> self . assertEqual ( simple_eval ( '<STR_LIT:a>' , { '<STR_LIT:a>' : <NUM_LIT:1> } ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( simple_eval ( '<STR_LIT>' ) , map ) <EOL> self . assertEqual ( simple_eval ( '<STR_LIT>' , { '<STR_LIT:a>' : { '<STR_LIT:c>' : <NUM_LIT:1> } , '<STR_LIT:b>' : '<STR_LIT:c>' } ) , <NUM_LIT:1> ) <EOL> def test_allow_name_lookup ( self ) : <EOL> """<STR_LIT>""" <EOL> self . assertEqual ( simple_eval ( '<STR_LIT:a>' , { '<STR_LIT:a>' : <NUM_LIT:1> } ) , <NUM_LIT:1> ) <EOL> def test_lookup_on_suspicious_types ( self ) : <EOL> class FakeDict ( object ) : <EOL> pass <EOL> with self . assertRaises ( ValueError ) : <EOL> simple_eval ( '<STR_LIT>' , { '<STR_LIT:a>' : FakeDict ( ) } ) <EOL> class TrickyDict ( dict ) : <EOL> def __getitem__ ( self , index ) : <EOL> self . fail ( "<STR_LIT>" ) <EOL> with self . assertRaises ( ValueError ) : <EOL> simple_eval ( '<STR_LIT>' , { '<STR_LIT:a>' : TrickyDict ( ) } ) <EOL> class SchrodingersDict ( dict ) : <EOL> def __getattribute__ ( inner_self , attr ) : <EOL> self . fail ( "<STR_LIT>" ) <EOL> with self . assertRaises ( ValueError ) : <EOL> simple_eval ( '<STR_LIT>' , { '<STR_LIT:a>' : SchrodingersDict ( ) } ) <EOL> class SchrodingersCatsDict ( dict ) : <EOL> def __getattr__ ( inner_self , attr ) : <EOL> self . fail ( "<STR_LIT>" ) <EOL> with self . assertRaises ( ValueError ) : <EOL> simple_eval ( '<STR_LIT>' , { '<STR_LIT:a>' : SchrodingersDict ( ) } ) <EOL> def test_operators_on_suspicious_types ( self ) : <EOL> class Spam ( numbers . Number ) : <EOL> def __add__ ( inner_self , other ) : <EOL> self . fail ( "<STR_LIT>" ) <EOL> with self . assertRaises ( ValueError ) : <EOL> simple_eval ( '<STR_LIT>' , { '<STR_LIT:a>' : Spam ( ) } ) <EOL> def test_operators_on_numbers ( self ) : <EOL> self . assertEqual ( simple_eval ( '<STR_LIT>' ) , - <NUM_LIT:2> ) <EOL> self . assertEqual ( simple_eval ( '<STR_LIT>' ) , <NUM_LIT:2> ) <EOL> self . assertEqual ( simple_eval ( '<STR_LIT>' , { '<STR_LIT:a>' : <NUM_LIT:1> } ) , - <NUM_LIT:1> ) <EOL> with self . assertRaises ( ValueError ) : <EOL> simple_eval ( '<STR_LIT>' ) <EOL> with self . assertRaises ( ValueError ) : <EOL> simple_eval ( '<STR_LIT>' ) <EOL> def test_function_calls_raise ( self ) : <EOL> with self . assertRaises ( ValueError ) : <EOL> simple_eval ( '<STR_LIT>' ) <EOL> def test_nonexistant_names_raise ( self ) : <EOL> with self . assertRaises ( EvaluationError ) : <EOL> simple_eval ( '<STR_LIT:a>' ) <EOL> def test_attribute_access ( self ) : <EOL> class Foo ( object ) : <EOL> abc = <NUM_LIT:1> <EOL> self . assertEqual ( simple_eval ( '<STR_LIT>' , { '<STR_LIT:foo>' : Foo ( ) } ) , <NUM_LIT:1> ) <EOL> class TestEvaluateCurrentExpression ( unittest . TestCase ) : <EOL> def assertEvaled ( self , line , value , ns = None ) : <EOL> assert line . count ( '<STR_LIT:|>' ) == <NUM_LIT:1> <EOL> cursor_offset = line . find ( '<STR_LIT:|>' ) <EOL> line = line . replace ( '<STR_LIT:|>' , '<STR_LIT>' ) <EOL> self . assertEqual ( evaluate_current_expression ( cursor_offset , line , ns ) , <EOL> value ) <EOL> def assertCannotEval ( self , line , ns = None ) : <EOL> assert line . count ( '<STR_LIT:|>' ) == <NUM_LIT:1> <EOL> cursor_offset = line . find ( '<STR_LIT:|>' ) <EOL> line = line . replace ( '<STR_LIT:|>' , '<STR_LIT>' ) <EOL> with self . assertRaises ( EvaluationError ) : <EOL> evaluate_current_expression ( cursor_offset , line , ns ) <EOL> def test_simple ( self ) : <EOL> self . assertEvaled ( '<STR_LIT>' , [ <NUM_LIT:1> ] ) <EOL> self . assertEvaled ( '<STR_LIT>' , [ <NUM_LIT:1> ] ) <EOL> self . assertEvaled ( '<STR_LIT>' , [ <NUM_LIT:1> ] ) <EOL> self . assertEvaled ( '<STR_LIT>' , [ <NUM_LIT:1> ] ) <EOL> self . assertEvaled ( '<STR_LIT>' , [ <NUM_LIT:1> ] ) <EOL> self . assertCannotEval ( '<STR_LIT>' , [ <NUM_LIT:1> ] ) <EOL> self . assertCannotEval ( '<STR_LIT>' , [ <NUM_LIT:1> ] ) <EOL> self . assertCannotEval ( '<STR_LIT>' , [ <NUM_LIT:1> ] ) <EOL> def test_nonsense ( self ) : <EOL> self . assertEvaled ( '<STR_LIT>' , [ <NUM_LIT:1> ] ) <EOL> self . assertEvaled ( '<STR_LIT>' , <NUM_LIT:2> ) <EOL> self . assertCannotEval ( '<STR_LIT>' ) <EOL> self . assertEvaled ( '<STR_LIT>' , '<STR_LIT:s>' ) <EOL> def test_with_namespace ( self ) : <EOL> self . assertEvaled ( '<STR_LIT>' , '<STR_LIT:d>' , { '<STR_LIT:a>' : '<STR_LIT>' } ) <EOL> self . assertCannotEval ( '<STR_LIT>' , { } ) <EOL> class A ( object ) : <EOL> a = '<STR_LIT:a>' <EOL> class B ( A ) : <EOL> b = '<STR_LIT:b>' <EOL> class Property ( object ) : <EOL> @ property <EOL> def prop ( self ) : <EOL> raise AssertionError ( '<STR_LIT>' ) <EOL> class Slots ( object ) : <EOL> __slots__ = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> if not py3 : <EOL> @ property <EOL> def s3 ( self ) : <EOL> raise AssertionError ( '<STR_LIT>' ) <EOL> class SlotsSubclass ( Slots ) : <EOL> @ property <EOL> def s4 ( self ) : <EOL> raise AssertionError ( '<STR_LIT>' ) <EOL> class OverriddenGetattr ( object ) : <EOL> def __getattr__ ( self , attr ) : <EOL> raise AssertionError ( '<STR_LIT>' ) <EOL> a = <NUM_LIT:1> <EOL> class OverriddenGetattribute ( object ) : <EOL> def __getattribute__ ( self , attr ) : <EOL> raise AssertionError ( '<STR_LIT>' ) <EOL> a = <NUM_LIT:1> <EOL> class OverriddenMRO ( object ) : <EOL> def __mro__ ( self ) : <EOL> raise AssertionError ( '<STR_LIT>' ) <EOL> a = <NUM_LIT:1> <EOL> member_descriptor = type ( Slots . s1 ) <EOL> class TestSafeGetAttribute ( unittest . TestCase ) : <EOL> def test_lookup_on_object ( self ) : <EOL> a = A ( ) <EOL> a . x = <NUM_LIT:1> <EOL> self . assertEquals ( safe_get_attribute_new_style ( a , '<STR_LIT:x>' ) , <NUM_LIT:1> ) <EOL> self . assertEquals ( safe_get_attribute_new_style ( a , '<STR_LIT:a>' ) , '<STR_LIT:a>' ) <EOL> b = B ( ) <EOL> b . y = <NUM_LIT:2> <EOL> self . assertEquals ( safe_get_attribute_new_style ( b , '<STR_LIT:y>' ) , <NUM_LIT:2> ) <EOL> self . assertEquals ( safe_get_attribute_new_style ( b , '<STR_LIT:a>' ) , '<STR_LIT:a>' ) <EOL> self . assertEquals ( safe_get_attribute_new_style ( b , '<STR_LIT:b>' ) , '<STR_LIT:b>' ) <EOL> def test_avoid_running_properties ( self ) : <EOL> p = Property ( ) <EOL> self . assertEquals ( safe_get_attribute_new_style ( p , '<STR_LIT>' ) , <EOL> Property . prop ) <EOL> @ unittest . skipIf ( py3 , '<STR_LIT>' ) <EOL> def test_raises_on_old_style_class ( self ) : <EOL> class Old : <EOL> pass <EOL> with self . assertRaises ( ValueError ) : <EOL> safe_get_attribute_new_style ( Old , '<STR_LIT>' ) <EOL> def test_lookup_with_slots ( self ) : <EOL> s = Slots ( ) <EOL> s . s1 = '<STR_LIT>' <EOL> self . assertEquals ( safe_get_attribute ( s , '<STR_LIT>' ) , '<STR_LIT>' ) <EOL> self . assertIsInstance ( safe_get_attribute_new_style ( s , '<STR_LIT>' ) , <EOL> member_descriptor ) <EOL> with self . assertRaises ( AttributeError ) : <EOL> safe_get_attribute ( s , '<STR_LIT>' ) <EOL> self . assertIsInstance ( safe_get_attribute_new_style ( s , '<STR_LIT>' ) , <EOL> member_descriptor ) <EOL> def test_lookup_on_slots_classes ( self ) : <EOL> sga = safe_get_attribute <EOL> s = SlotsSubclass ( ) <EOL> self . assertIsInstance ( sga ( Slots , '<STR_LIT>' ) , member_descriptor ) <EOL> self . assertIsInstance ( sga ( SlotsSubclass , '<STR_LIT>' ) , member_descriptor ) <EOL> self . assertIsInstance ( sga ( SlotsSubclass , '<STR_LIT>' ) , property ) <EOL> self . assertIsInstance ( sga ( s , '<STR_LIT>' ) , property ) <EOL> @ unittest . skipIf ( py3 , "<STR_LIT>" ) <EOL> def test_lookup_with_property_and_slots ( self ) : <EOL> sga = safe_get_attribute <EOL> s = SlotsSubclass ( ) <EOL> self . assertIsInstance ( sga ( Slots , '<STR_LIT>' ) , property ) <EOL> self . assertEquals ( safe_get_attribute ( s , '<STR_LIT>' ) , <EOL> Slots . __dict__ [ '<STR_LIT>' ] ) <EOL> self . assertIsInstance ( sga ( SlotsSubclass , '<STR_LIT>' ) , property ) <EOL> def test_lookup_on_overridden_methods ( self ) : <EOL> sga = safe_get_attribute <EOL> self . assertEqual ( sga ( OverriddenGetattr ( ) , '<STR_LIT:a>' ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( sga ( OverriddenGetattribute ( ) , '<STR_LIT:a>' ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( sga ( OverriddenMRO ( ) , '<STR_LIT:a>' ) , <NUM_LIT:1> ) <EOL> with self . assertRaises ( AttributeError ) : <EOL> sga ( OverriddenGetattr ( ) , '<STR_LIT:b>' ) <EOL> with self . assertRaises ( AttributeError ) : <EOL> sga ( OverriddenGetattribute ( ) , '<STR_LIT:b>' ) <EOL> with self . assertRaises ( AttributeError ) : <EOL> sga ( OverriddenMRO ( ) , '<STR_LIT:b>' ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> unittest . main ( ) </s>
