11"""This module trys to support builtin types and functions."""
22import inspect
3+ try :
4+ raw_input
5+ except NameError :
6+ raw_input = input
37
48import rope .base .evaluate
5- from rope .base import pynames , pyobjects , arguments , utils , ast
9+ from rope .base .utils import pycompat
10+ from rope .base import pynames , pyobjects , arguments , utils
611
712
813class BuiltinModule (pyobjects .AbstractModule ):
@@ -32,7 +37,7 @@ def attributes(self):
3237 result .update (self .initial )
3338 if self .pycore is not None :
3439 submodules = self .pycore ._builtin_submodules (self .name )
35- for name , module in submodules .iteritems ():
40+ for name , module in submodules .items ():
3641 result [name ] = rope .base .builtins .BuiltinName (module )
3742 return result
3843
@@ -266,7 +271,10 @@ def __init__(self, holding=None):
266271 # Getting methods
267272 collector ('__getitem__' , function = self ._list_get )
268273 collector ('pop' , function = self ._list_get )
269- collector ('__getslice__' , function = self ._self_get )
274+ try :
275+ collector ('__getslice__' , function = self ._list_get )
276+ except AttributeError :
277+ pass
270278
271279 super (List , self ).__init__ (list , collector .attributes )
272280
@@ -290,6 +298,10 @@ def _self_set(self, context):
290298
291299 def _list_get (self , context ):
292300 if self .holding is not None :
301+ args = context .get_arguments (['self' , 'key' ])
302+ if (len (args ) > 1 and args [1 ] is not None and
303+ args [1 ].get_type () == builtins ['slice' ].get_object ()):
304+ return get_list (self .holding )
293305 return self .holding
294306 return context .get_per_name ()
295307
@@ -407,7 +419,7 @@ def __init__(self, *objects):
407419 if objects :
408420 first = objects [0 ]
409421 attributes = {
410- '__getitem__' : BuiltinName (BuiltinFunction (first )),
422+ '__getitem__' : BuiltinName (BuiltinFunction (first )), # TODO: add slice support
411423 '__getslice__' :
412424 BuiltinName (BuiltinFunction (pyobjects .PyObject (self ))),
413425 '__new__' : BuiltinName (BuiltinFunction (function = self ._new_tuple )),
@@ -487,14 +499,21 @@ def __init__(self):
487499 collector = _AttributeCollector (str )
488500 collector ('__iter__' , get_iterator (self_object ), check_existence = False )
489501
490- self_methods = ['__getitem__' , '__getslice__' , ' capitalize' , 'center' ,
491- 'decode' , ' encode' , 'expandtabs' , 'join' , 'ljust' ,
502+ self_methods = ['__getitem__' , 'capitalize' , 'center' ,
503+ 'encode' , 'expandtabs' , 'join' , 'ljust' ,
492504 'lower' , 'lstrip' , 'replace' , 'rjust' , 'rstrip' ,
493505 'strip' , 'swapcase' , 'title' , 'translate' , 'upper' ,
494506 'zfill' ]
495507 for method in self_methods :
496508 collector (method , self_object )
497509
510+ py2_self_methods = ["__getslice__" , "decode" ]
511+ for method in py2_self_methods :
512+ try :
513+ collector (method , self_object )
514+ except AttributeError :
515+ pass
516+
498517 for method in ['rsplit' , 'split' , 'splitlines' ]:
499518 collector (method , get_list (self_object ))
500519
@@ -568,7 +587,7 @@ def __init__(self):
568587 attributes = {}
569588
570589 def add (name , returned = None , function = None ):
571- builtin = getattr (file , name , None )
590+ builtin = getattr (open , name , None )
572591 attributes [name ] = BuiltinName (
573592 BuiltinFunction (returned = returned , function = function ,
574593 builtin = builtin ))
@@ -578,7 +597,7 @@ def add(name, returned=None, function=None):
578597 for method in ['close' , 'flush' , 'lineno' , 'isatty' , 'seek' , 'tell' ,
579598 'truncate' , 'write' , 'writelines' ]:
580599 add (method )
581- super (File , self ).__init__ (file , attributes )
600+ super (File , self ).__init__ (open , attributes )
582601
583602
584603get_file = _create_builtin_getter (File )
@@ -642,12 +661,12 @@ def get_name(self):
642661 return 'lambda'
643662
644663 def get_param_names (self , special_args = True ):
645- result = [node . id for node in self .arguments .args
646- if isinstance (node , ast . Name )]
664+ result = [pycompat . get_ast_arg_arg ( node ) for node in self .arguments .args
665+ if isinstance (node , pycompat . ast_arg_type )]
647666 if self .arguments .vararg :
648- result .append ('*' + self .arguments .vararg )
667+ result .append ('*' + pycompat . get_ast_arg_arg ( self .arguments .vararg ) )
649668 if self .arguments .kwarg :
650- result .append ('**' + self .arguments .kwarg )
669+ result .append ('**' + pycompat . get_ast_arg_arg ( self .arguments .kwarg ) )
651670 return result
652671
653672 @property
@@ -787,4 +806,4 @@ def _input_function(args):
787806 builtin = raw_input )),
788807}
789808
790- builtins = BuiltinModule ('__builtin__' , initial = _initial_builtins )
809+ builtins = BuiltinModule (pycompat . builtins . __name__ , initial = _initial_builtins )
0 commit comments