@@ -180,6 +180,18 @@ class Definition(Binding):
180180 """
181181
182182
183+ class Builtin (Definition ):
184+ """A definition created for all Python builtins."""
185+
186+ def __init__ (self , name ):
187+ super (Builtin , self ).__init__ (name , None )
188+
189+ def __repr__ (self ):
190+ return '<%s object %r at 0x%x>' % (self .__class__ .__name__ ,
191+ self .name ,
192+ id (self ))
193+
194+
183195class UnhandledKeyType (object ):
184196 """
185197 A dictionary key of a type that we cannot or do not check for duplicates.
@@ -516,6 +528,8 @@ def __init__(self, tree, filename='(none)', builtins=None,
516528 raise RuntimeError ('No scope implemented for the node %r' % tree )
517529 self .exceptHandlers = [()]
518530 self .root = tree
531+ for builtin in self .builtIns :
532+ self .addBinding (None , Builtin (builtin ))
519533 self .handleChildren (tree )
520534 self .runDeferred (self ._deferredFunctions )
521535 # Set _deferredFunctions to None so that deferFunction will fail
@@ -699,7 +713,8 @@ def addBinding(self, node, value):
699713 break
700714 existing = scope .get (value .name )
701715
702- if existing and not self .differentForks (node , existing .source ):
716+ if (existing and not isinstance (existing , Builtin ) and
717+ not self .differentForks (node , existing .source )):
703718
704719 parent_stmt = self .getParent (value .source )
705720 if isinstance (existing , Importation ) and isinstance (parent_stmt , ast .For ):
@@ -765,10 +780,6 @@ def handleNodeLoad(self, node):
765780 if in_generators is not False :
766781 in_generators = isinstance (scope , GeneratorScope )
767782
768- # look in the built-ins
769- if name in self .builtIns :
770- return
771-
772783 if importStarred :
773784 from_list = []
774785
@@ -943,9 +954,7 @@ def handleDoctests(self, node):
943954 self .scopeStack = [self .scopeStack [0 ]]
944955 node_offset = self .offset or (0 , 0 )
945956 self .pushScope (DoctestScope )
946- underscore_in_builtins = '_' in self .builtIns
947- if not underscore_in_builtins :
948- self .builtIns .add ('_' )
957+ self .addBinding (None , Builtin ('_' ))
949958 for example in examples :
950959 try :
951960 tree = compile (example .source , "<doctest>" , "exec" , ast .PyCF_ONLY_AST )
@@ -961,8 +970,6 @@ def handleDoctests(self, node):
961970 node_offset [1 ] + example .indent + 4 )
962971 self .handleChildren (tree )
963972 self .offset = node_offset
964- if not underscore_in_builtins :
965- self .builtIns .remove ('_' )
966973 self .popScope ()
967974 self .scopeStack = saved_stack
968975
0 commit comments