mirror of
https://github.com/davidhalter/jedi.git
synced 2025-12-10 07:41:51 +08:00
fast_parent_copy should also change the parent of NameParts.
This commit is contained in:
@@ -10,6 +10,7 @@ def fast_parent_copy(obj):
|
|||||||
Much, much faster than copy.deepcopy, but just for certain elements.
|
Much, much faster than copy.deepcopy, but just for certain elements.
|
||||||
"""
|
"""
|
||||||
new_elements = {}
|
new_elements = {}
|
||||||
|
accept = (pr.Simple, pr.NamePart)
|
||||||
|
|
||||||
def recursion(obj):
|
def recursion(obj):
|
||||||
if isinstance(obj, pr.Statement):
|
if isinstance(obj, pr.Statement):
|
||||||
@@ -44,23 +45,30 @@ def fast_parent_copy(obj):
|
|||||||
setattr(new_obj, key, new_elements[value])
|
setattr(new_obj, key, new_elements[value])
|
||||||
elif key in ['parent_function', 'use_as_parent', '_sub_module']:
|
elif key in ['parent_function', 'use_as_parent', '_sub_module']:
|
||||||
continue
|
continue
|
||||||
elif isinstance(value, list):
|
elif isinstance(value, (list, tuple)):
|
||||||
setattr(new_obj, key, list_rec(value))
|
setattr(new_obj, key, list_or_tuple_rec(value))
|
||||||
elif isinstance(value, pr.Simple):
|
elif isinstance(value, accept):
|
||||||
try: # because of the circular Flow.previous/Flow.next
|
try: # because of the circular Flow.previous/Flow.next
|
||||||
setattr(new_obj, key, new_elements[value])
|
setattr(new_obj, key, new_elements[value])
|
||||||
except KeyError:
|
except KeyError:
|
||||||
setattr(new_obj, key, recursion(value))
|
setattr(new_obj, key, recursion(value))
|
||||||
return new_obj
|
return new_obj
|
||||||
|
|
||||||
def list_rec(list_obj):
|
def list_or_tuple_rec(array_obj):
|
||||||
copied_list = list_obj[:] # lists, tuples, strings, unicode
|
if isinstance(array_obj, tuple):
|
||||||
for i, el in enumerate(copied_list):
|
copied_array = list(array_obj)
|
||||||
if isinstance(el, pr.Simple):
|
else:
|
||||||
copied_list[i] = recursion(el)
|
copied_array = array_obj[:] # lists, tuples, strings, unicode
|
||||||
elif isinstance(el, list):
|
for i, el in enumerate(copied_array):
|
||||||
copied_list[i] = list_rec(el)
|
if isinstance(el, accept):
|
||||||
return copied_list
|
copied_array[i] = recursion(el)
|
||||||
|
elif isinstance(el, (tuple, list)):
|
||||||
|
copied_array[i] = list_or_tuple_rec(el)
|
||||||
|
|
||||||
|
if isinstance(array_obj, tuple):
|
||||||
|
return tuple(copied_array)
|
||||||
|
return copied_array
|
||||||
|
|
||||||
return recursion(obj)
|
return recursion(obj)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
10
test/test_evaluate/test_helpers.py
Normal file
10
test/test_evaluate/test_helpers.py
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
from jedi.evaluate.helpers import fast_parent_copy
|
||||||
|
from jedi.parser import representation as pr
|
||||||
|
|
||||||
|
|
||||||
|
def test_fast_parent_copy():
|
||||||
|
name = pr.Name(object, [('hallo', (0, 0))], (0, 0), (0, 0))
|
||||||
|
|
||||||
|
# fast parent copy should switch parent
|
||||||
|
new_name = fast_parent_copy(name)
|
||||||
|
assert new_name.names[0].parent == new_name
|
||||||
Reference in New Issue
Block a user