<s> from __future__ import absolute_import <EOL> from . Visitor import ScopeTrackingTransform <EOL> from . Nodes import StatListNode , SingleAssignmentNode , CFuncDefNode , DefNode <EOL> from . ExprNodes import DictNode , DictItemNode , NameNode , UnicodeNode <EOL> from . PyrexTypes import py_object_type <EOL> from . StringEncoding import EncodedString <EOL> from . import Symtab <EOL> class AutoTestDictTransform ( ScopeTrackingTransform ) : <EOL> blacklist = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ] <EOL> def visit_ModuleNode ( self , node ) : <EOL> if node . is_pxd : <EOL> return node <EOL> self . scope_type = '<STR_LIT>' <EOL> self . scope_node = node <EOL> if not self . current_directives [ '<STR_LIT>' ] : <EOL> return node <EOL> self . all_docstrings = self . current_directives [ '<STR_LIT>' ] <EOL> self . cdef_docstrings = self . all_docstrings or self . current_directives [ '<STR_LIT>' ] <EOL> assert isinstance ( node . body , StatListNode ) <EOL> if u'<STR_LIT>' in node . scope . entries : <EOL> return node <EOL> pos = node . pos <EOL> self . tests = [ ] <EOL> self . testspos = node . pos <EOL> test_dict_entry = node . scope . declare_var ( EncodedString ( u'<STR_LIT>' ) , <EOL> py_object_type , <EOL> pos , <EOL> visibility = '<STR_LIT>' ) <EOL> create_test_dict_assignment = SingleAssignmentNode ( pos , <EOL> lhs = NameNode ( pos , name = EncodedString ( u'<STR_LIT>' ) , <EOL> entry = test_dict_entry ) , <EOL> rhs = DictNode ( pos , key_value_pairs = self . tests ) ) <EOL> self . visitchildren ( node ) <EOL> node . body . stats . append ( create_test_dict_assignment ) <EOL> return node <EOL> def add_test ( self , testpos , path , doctest ) : <EOL> pos = self . testspos <EOL> keystr = u'<STR_LIT>' % ( path , testpos [ <NUM_LIT:1> ] ) <EOL> key = UnicodeNode ( pos , value = EncodedString ( keystr ) ) <EOL> value = UnicodeNode ( pos , value = doctest ) <EOL> self . tests . append ( DictItemNode ( pos , key = key , value = value ) ) <EOL> def visit_ExprNode ( self , node ) : <EOL> return node <EOL> def visit_FuncDefNode ( self , node ) : <EOL> if not node . doc or ( isinstance ( node , DefNode ) and node . fused_py_func ) : <EOL> return node <EOL> if not self . cdef_docstrings : <EOL> if isinstance ( node , CFuncDefNode ) and not node . py_func : <EOL> return node <EOL> if not self . all_docstrings and '<STR_LIT>' not in node . doc : <EOL> return node <EOL> pos = self . testspos <EOL> if self . scope_type == '<STR_LIT>' : <EOL> path = node . entry . name <EOL> elif self . scope_type in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> if isinstance ( node , CFuncDefNode ) : <EOL> if node . py_func is not None : <EOL> name = node . py_func . name <EOL> else : <EOL> name = node . entry . name <EOL> else : <EOL> name = node . name <EOL> if self . scope_type == '<STR_LIT>' and name in self . blacklist : <EOL> return node <EOL> if self . scope_type == '<STR_LIT>' : <EOL> class_name = self . scope_node . name <EOL> else : <EOL> class_name = self . scope_node . class_name <EOL> if isinstance ( node . entry . scope , Symtab . PropertyScope ) : <EOL> property_method_name = node . entry . scope . name <EOL> path = "<STR_LIT>" % ( class_name , node . entry . scope . name , <EOL> node . entry . name ) <EOL> else : <EOL> path = "<STR_LIT>" % ( class_name , node . entry . name ) <EOL> else : <EOL> assert False <EOL> self . add_test ( node . pos , path , node . doc ) <EOL> return node </s>
