@@ -180,6 +180,18 @@ class Definition(Binding):
180
180
"""
181
181
182
182
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
+
183
195
class UnhandledKeyType (object ):
184
196
"""
185
197
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,
516
528
raise RuntimeError ('No scope implemented for the node %r' % tree )
517
529
self .exceptHandlers = [()]
518
530
self .root = tree
531
+ for builtin in self .builtIns :
532
+ self .addBinding (None , Builtin (builtin ))
519
533
self .handleChildren (tree )
520
534
self .runDeferred (self ._deferredFunctions )
521
535
# Set _deferredFunctions to None so that deferFunction will fail
@@ -699,7 +713,8 @@ def addBinding(self, node, value):
699
713
break
700
714
existing = scope .get (value .name )
701
715
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 )):
703
718
704
719
parent_stmt = self .getParent (value .source )
705
720
if isinstance (existing , Importation ) and isinstance (parent_stmt , ast .For ):
@@ -765,10 +780,6 @@ def handleNodeLoad(self, node):
765
780
if in_generators is not False :
766
781
in_generators = isinstance (scope , GeneratorScope )
767
782
768
- # look in the built-ins
769
- if name in self .builtIns :
770
- return
771
-
772
783
if importStarred :
773
784
from_list = []
774
785
@@ -943,9 +954,7 @@ def handleDoctests(self, node):
943
954
self .scopeStack = [self .scopeStack [0 ]]
944
955
node_offset = self .offset or (0 , 0 )
945
956
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 ('_' ))
949
958
for example in examples :
950
959
try :
951
960
tree = compile (example .source , "<doctest>" , "exec" , ast .PyCF_ONLY_AST )
@@ -961,8 +970,6 @@ def handleDoctests(self, node):
961
970
node_offset [1 ] + example .indent + 4 )
962
971
self .handleChildren (tree )
963
972
self .offset = node_offset
964
- if not underscore_in_builtins :
965
- self .builtIns .remove ('_' )
966
973
self .popScope ()
967
974
self .scopeStack = saved_stack
968
975
0 commit comments