a hopefully simple integration of PyObject into Instance

This commit is contained in:
Dave Halter
2014-01-11 01:48:59 +01:00
parent 8337f77886
commit 19fa320c88
3 changed files with 36 additions and 22 deletions
+6 -3
View File
@@ -361,7 +361,6 @@ class Evaluator(object):
result = typ.get_index_types(current) result = typ.get_index_types(current)
elif current.type not in [pr.Array.DICT]: elif current.type not in [pr.Array.DICT]:
# Scope must be a class or func - make an instance or execution. # Scope must be a class or func - make an instance or execution.
debug.dbg('exe', typ)
result = self.execute(typ, current) result = self.execute(typ, current)
else: else:
# Curly braces are not allowed, because they make no sense. # Curly braces are not allowed, because they make no sense.
@@ -382,13 +381,17 @@ class Evaluator(object):
if obj.isinstance(er.Function): if obj.isinstance(er.Function):
obj = obj.get_decorated_func() obj = obj.get_decorated_func()
debug.dbg('execute:', obj, params)
try: try:
return stdlib.execute(self, obj, params) return stdlib.execute(self, obj, params)
except stdlib.NotInStdLib: except stdlib.NotInStdLib:
pass pass
if isinstance(obj, compiled.PyObject): if isinstance(obj, compiled.PyObject):
return list(obj.execute(self, params)) if obj.is_executable_class():
return [er.Instance(self, obj, params)]
else:
return list(obj.execute_function(self, params))
elif obj.isinstance(er.Class): elif obj.isinstance(er.Class):
# There maybe executions of executions. # There maybe executions of executions.
return [er.Instance(self, obj, params)] return [er.Instance(self, obj, params)]
@@ -409,7 +412,7 @@ class Evaluator(object):
else: else:
stmts = er.FunctionExecution(self, obj, params).get_return_types(evaluate_generator) stmts = er.FunctionExecution(self, obj, params).get_return_types(evaluate_generator)
debug.dbg('execute: %s in %s' % (stmts, obj)) debug.dbg('execute result: %s in %s' % (stmts, obj))
return imports.strip_imports(self, stmts) return imports.strip_imports(self, stmts)
def goto(self, stmt, call_path=None): def goto(self, stmt, call_path=None):
+28 -19
View File
@@ -19,15 +19,16 @@ from . import fake
# if not hasattr(func, "__name__"): # if not hasattr(func, "__name__"):
class PyObject(Base): class PyObject(Base):
# comply with the parser
start_pos = 0, 0
asserts = []
def __init__(self, obj, parent=None, instantiated=False): def __init__(self, obj, parent=None, instantiated=False):
self.obj = obj self.obj = obj
self.parent = parent self.parent = parent
self.instantiated = instantiated self.instantiated = instantiated
self.doc = inspect.getdoc(obj) self.doc = inspect.getdoc(obj)
# comply with the parser
self.start_pos = 0, 0
def __repr__(self): def __repr__(self):
return '<%s: %s>' % (type(self).__name__, self.obj) return '<%s: %s>' % (type(self).__name__, self.obj)
@@ -52,6 +53,9 @@ class PyObject(Base):
or inspect.ismethoddescriptor(cls): or inspect.ismethoddescriptor(cls):
return 'def' return 'def'
def is_executable_class(self):
return inspect.isclass(self.obj)
@underscore_memoization @underscore_memoization
def _cls(self): def _cls(self):
# Ensures that a PyObject is returned that is not an instance (like list) # Ensures that a PyObject is returned that is not an instance (like list)
@@ -64,28 +68,33 @@ class PyObject(Base):
for name in dir(cls.obj): for name in dir(cls.obj):
yield PyName(cls, name) yield PyName(cls, name)
def instance_names(self):
# TODO REMOVE (temporary until the Instance method is removed)
return self.get_defined_names()
def get_subscope_by_name(self, name):
if name in dir(self._cls().obj):
return PyName(self._cls, name).parent
else:
raise KeyError("CompiledObject doesn't have an attribute '%s'." % name)
@property @property
def name(self): def name(self):
# might not exist sometimes (raises AttributeError) # might not exist sometimes (raises AttributeError)
return self._cls().obj.__name__ return self._cls().obj.__name__
def execute(self, evaluator, params): def execute_function(self, evaluator, params):
t = self.type() for name in self._parse_function_doc()[1].split():
if t == 'class': try:
if not self.instantiated: bltn_obj = create(getattr(_builtins, name), builtin, module=builtin)
yield PyObject(self.obj, self.parent, True) except AttributeError:
elif t == 'def': continue
for name in self._parse_function_doc()[1].split(): else:
try: if isinstance(bltn_obj, PyObject):
bltn_obj = create(getattr(_builtins, name), builtin, module=builtin) yield bltn_obj
except AttributeError:
continue
else: else:
if isinstance(bltn_obj, PyObject): for result in evaluator.execute(bltn_obj, params):
yield bltn_obj yield result
else:
for result in evaluator.execute(bltn_obj, params):
yield result
def get_self_attributes(self): def get_self_attributes(self):
return [] # Instance compatibility return [] # Instance compatibility
+2
View File
@@ -87,6 +87,8 @@ class Instance(use_metaclass(CachedMetaClass, Executable)):
n.names = n.names[1:] n.names = n.names[1:]
names.append(InstanceElement(self._evaluator, self, n)) names.append(InstanceElement(self._evaluator, self, n))
if isinstance(self.base, compiled.PyObject):
return []
names = [] names = []
# This loop adds the names of the self object, copies them and removes # This loop adds the names of the self object, copies them and removes
# the self. # the self.