literal classes mostly working

This commit is contained in:
David Halter
2013-09-05 23:45:56 +04:30
parent fc5fdf929a
commit 1b5f4f5e0b
5 changed files with 20 additions and 22 deletions

View File

@@ -563,7 +563,7 @@ def _check_isinstance_type(stmt, search_name):
# this might be removed if we analyze and, etc
assert len(commands) == 1
call = commands[0]
assert type(call) is pr.Call and str(call.name) == 'isinstance'
assert isinstance(call, pr.Call) and str(call.name) == 'isinstance'
assert bool(call.execution)
# isinstance check

View File

@@ -477,7 +477,7 @@ def check_getattr(inst, name_str):
result = []
# str is important to lose the NamePart!
module = builtin.Builtin.scope
name = pr.Call(module, str(name_str), pr.Call.STRING, (0, 0), (0, 0), inst)
name = pr.String(module, str(name_str), (0, 0), (0, 0), inst)
with common.ignored(KeyError):
result = inst.execute_subscope_by_name('__getattr__', [name])
if not result:
@@ -701,7 +701,7 @@ def follow_call(call):
def follow_call_path(path, scope, position):
"""Follows a path generated by `pr.Call.generate_call_path()`"""
"""Follows a path generated by `pr.StatementElement.generate_call_path()`"""
current = next(path)
if isinstance(current, pr.Array):
@@ -712,14 +712,10 @@ def follow_call_path(path, scope, position):
scopes = find_name(scope, current, position=position,
search_global=True)
else:
if current.type in (pr.Call.STRING, pr.Call.NUMBER):
t = type(current.name).__name__
scopes = find_name(builtin.Builtin.scope, t)
else:
debug.warning('unknown type:', current.type, current)
scopes = []
# for pr.Literal
scopes = find_name(builtin.Builtin.scope, current.type_as_string())
# Make instances of those number/string objects.
scopes = [er.Instance(s, (current.name,)) for s in scopes]
scopes = [er.Instance(s, (current.value,)) for s in scopes]
result = imports.strip_imports(scopes)
return follow_paths(path, result, scope, position=position)

View File

@@ -860,7 +860,7 @@ class Array(use_metaclass(cache.CachedMetaClass, pr.Base)):
if len(key_commands) != 1: # cannot deal with complex strings
continue
key = key_commands[0]
if isinstance(key, pr.Call) and key.type == pr.Call.STRING:
if isinstance(key, pr.String):
str_key = key.name
elif isinstance(key, pr.Name):
str_key = str(key)

View File

@@ -47,14 +47,14 @@ def fast_parent_copy(obj):
continue
elif isinstance(value, list):
setattr(new_obj, key, list_rec(value))
elif isinstance(value, (pr.Simple, pr.Call)):
elif isinstance(value, pr.Simple):
setattr(new_obj, key, recursion(value))
return new_obj
def list_rec(list_obj):
copied_list = list_obj[:] # lists, tuples, strings, unicode
for i, el in enumerate(copied_list):
if isinstance(el, (pr.Simple, pr.Call)):
if isinstance(el, pr.Simple):
copied_list[i] = recursion(el)
elif isinstance(el, list):
copied_list[i] = list_rec(el)
@@ -109,7 +109,7 @@ def array_for_pos(stmt, pos, array_types=None):
arr = None
if isinstance(command, pr.Array):
arr, index = search_array(command, pos)
elif isinstance(command, pr.Call):
elif isinstance(command, pr.StatementElement):
arr, index = search_call(command, pos)
if arr is not None:
return arr, index
@@ -128,7 +128,7 @@ def search_call_signatures(stmt, pos):
while isinstance(call.parent, pr.Call):
call = call.parent
arr.parent.execution = None
return call if call.type == pr.Call.NAME else None, index, False
return call if isinstance(call, pr.Name) else None, index, False
return None, 0, False

View File

@@ -807,8 +807,7 @@ class Statement(Simple):
for stmt in call:
search_calls(stmt.get_commands())
elif isinstance(call, Call):
if call.type == Call.NAME:
self._set_vars.append(call.name)
self._set_vars.append(call.name)
for calls, operation in self.assignment_details:
search_calls(calls)
@@ -1138,7 +1137,7 @@ class Param(Statement):
class StatementElement(Simple):
def __init__(self, module, start_pos, end_pos, parent):
super(type(self), self).__init__(module, start_pos, end_pos)
super(StatementElement, self).__init__(module, start_pos, end_pos)
# parent is not the oposite of next. The parent of c: a = [b.c] would
# be an array.
@@ -1192,7 +1191,7 @@ class StatementElement(Simple):
class Call(StatementElement):
def __init__(self, module, name, start_pos, end_pos, parent=None):
super(type(self), self).__init__(module, start_pos, end_pos, parent)
super(Call, self).__init__(module, start_pos, end_pos, parent)
self.name = name
def get_code(self):
@@ -1204,12 +1203,15 @@ class Call(StatementElement):
class Literal(StatementElement):
def __init__(self, module, literal, start_pos, end_pos, parent=None):
super(type(self), self).__init__(module, start_pos, end_pos, parent)
super(Literal, self).__init__(module, start_pos, end_pos, parent)
self.literal = literal
self.value = literal_eval(literal)
def get_code(self):
return self.literal + super(Call, self).get_code()
return self.literal + super(Literal, self).get_code()
def type_as_string(self):
return type(self.value).__name__
def __repr__(self):
return "<%s: %s>" % (type(self).__name__, self.literal)
@@ -1219,7 +1221,7 @@ class String(Literal):
pass
class Number(Call):
class Number(Literal):
pass