resolved parent issue of params -> generators working now! / fixed path problem with tests

This commit is contained in:
David Halter
2012-05-12 23:29:07 +02:00
parent 1fb11dd178
commit 22d83e8518
8 changed files with 55 additions and 33 deletions

View File

@@ -5,6 +5,9 @@ import os
import debug import debug
import parsing import parsing
module_find_path = sys.path[1:]
class CachedModule(object): class CachedModule(object):
cache = {} cache = {}
@@ -32,7 +35,7 @@ class CachedModule(object):
def _load_module(self): def _load_module(self):
source = self._get_source() source = self._get_source()
self._parser = parsing.PyFuzzyParser(source, self.name) self._parser = parsing.PyFuzzyParser(source, self.path or self.name)
#except: #except:
# debug.warning('not possible to resolve', self.name, source) # debug.warning('not possible to resolve', self.name, source)
#open('builtin_fail', 'w').write(code) #open('builtin_fail', 'w').write(code)
@@ -70,7 +73,7 @@ class Parser(CachedModule):
} }
module_cache = {} module_cache = {}
def __init__(self, path=None, name=None, sys_path=sys.path): def __init__(self, path=None, name=None, sys_path=module_find_path):
if not name: if not name:
name = os.path.basename(path) name = os.path.basename(path)
name = name.rpartition('.')[0] # cut file type (normally .so) name = name.rpartition('.')[0] # cut file type (normally .so)
@@ -116,7 +119,7 @@ class Parser(CachedModule):
positions = [m.start() for m in matches] positions = [m.start() for m in matches]
for i, pos in enumerate(positions): for i, pos in enumerate(positions):
try: try:
code_block = code[pos:positions[i+1]] code_block = code[pos:positions[i + 1]]
except IndexError: except IndexError:
code_block = code[pos:len(code)] code_block = code[pos:len(code)]
structure_name = matches[i].group(1) structure_name = matches[i].group(1)
@@ -125,7 +128,7 @@ class Parser(CachedModule):
self.mixin_funcs[name] = code_block self.mixin_funcs[name] = code_block
else: else:
raise NotImplementedError raise NotImplementedError
print code_block #print code_block
def _generate_code(self, scope, depth=0): def _generate_code(self, scope, depth=0):
""" """

View File

@@ -99,6 +99,7 @@ class Executable(object):
calls.keys = keys calls.keys = keys
calls.type = array_type calls.type = array_type
new_param = copy.copy(param) new_param = copy.copy(param)
new_param.parent = self.var_args.parent_stmt
new_param._assignment_calls_calculated = True new_param._assignment_calls_calculated = True
new_param._assignment_calls = calls new_param._assignment_calls = calls
name = copy.copy(param.get_name()) name = copy.copy(param.get_name())
@@ -400,6 +401,9 @@ class Execution(Executable):
if isinstance(self.base, Class): if isinstance(self.base, Class):
# there maybe executions of executions # there maybe executions of executions
stmts = [Instance(self.base, self.var_args)] stmts = [Instance(self.base, self.var_args)]
elif isinstance(self.base, Generator):
return Execution(self.base.func).get_return_types(True)
pass
else: else:
func = self.process_decorators() func = self.process_decorators()
@@ -407,7 +411,7 @@ class Execution(Executable):
# don't do this with exceptions, as usual, because some deeper # don't do this with exceptions, as usual, because some deeper
# exceptions could be catched - and I wouldn't know what happened. # exceptions could be catched - and I wouldn't know what happened.
if hasattr(func, 'returns'): if hasattr(func, 'returns'):
if func.is_generator: if func.is_generator and not evaluate_generator:
return [Generator(func)] return [Generator(func)]
else: else:
self.set_param_cb(func) self.set_param_cb(func)
@@ -438,13 +442,27 @@ class Execution(Executable):
class Generator(object): class Generator(object):
def __init__(self, execution): def __init__(self, func):
super(Generator, self).__init__() super(Generator, self).__init__()
self.execution = execution self.func = func
def get_defined_names(self): def get_defined_names(self):
return [] """
Returns a list of GeneratorObject, which can return the content of a
generator
"""
names = []
for n in ['__next__', 'send']:
# the name for the `next` function
name = parsing.Name([n], 0, 0, 0)
name.parent = self
names.append(name)
return names
@property
def parent(self):
# TODO add generator names (__next__, send, close, throw, next?) # TODO add generator names (__next__, send, close, throw, next?)
return self.func.parent
#self.execution.get_return_types() #self.execution.get_return_types()
@@ -526,10 +544,6 @@ class ArrayElement(object):
def parent(self): def parent(self):
raise NotImplementedError("This shouldn't happen") raise NotImplementedError("This shouldn't happen")
@property
def returns(self):
return self.name.parent.returns
@property @property
def names(self): def names(self):
return self.name.names return self.name.names
@@ -822,7 +836,7 @@ def follow_call(scope, call):
# reset the position, when imports where stripped # reset the position, when imports where stripped
position = None position = None
debug.dbg('call before result %s, current %s, scope %s' debug.dbg('call before result %s, current "%s", scope %s'
% (result, current, scope)) % (result, current, scope))
result = follow_paths(path, result, position=position) result = follow_paths(path, result, position=position)

View File

@@ -5,7 +5,7 @@ import functions
functions.debug.debug_function = functions.debug.print_to_stdout functions.debug.debug_function = functions.debug.print_to_stdout
functions.debug.ignored_modules = ['parsing', 'builtin'] functions.debug.ignored_modules = ['parsing', 'builtin']
#functions.debug.ignored_modules = ['parsing', 'builtin', 'evaluate', 'modules'] #functions.debug.ignored_modules = ['parsing', 'builtin', 'evaluate', 'modules']
functions.modules.module_find_path.insert(0, '.') functions.modules.builtin.module_find_path.insert(0, '.')
f_name = 'parsetest.py' f_name = 'parsetest.py'
#f_name = 'test/completion/classes.py' #f_name = 'test/completion/classes.py'

View File

@@ -1,5 +1,5 @@
def next(iterator, default=None): def next(iterator, default=1):
if hasattr("next"): if hasattr("next"):
return iterator.next() return iterator.next()
else: else:

View File

@@ -1,5 +1,4 @@
import imp import imp
import sys
import os import os
import debug import debug
@@ -8,7 +7,6 @@ import builtin
files = {} files = {}
load_module_cb = None load_module_cb = None
module_find_path = sys.path[1:]
class ModuleNotFound(Exception): class ModuleNotFound(Exception):
@@ -64,12 +62,12 @@ def find_module(current_module, point_path):
i = imp.find_module(string, path) i = imp.find_module(string, path)
except ImportError: except ImportError:
# find builtins (ommit path): # find builtins (ommit path):
i = imp.find_module(string, module_find_path) i = imp.find_module(string, builtin.module_find_path)
return i return i
# TODO handle relative paths - they are included in the import object # TODO handle relative paths - they are included in the import object
current_namespace = None current_namespace = None
module_find_path.insert(0, os.path.dirname(current_module.path)) builtin.module_find_path.insert(0, os.path.dirname(current_module.path))
# now execute those paths # now execute those paths
rest = [] rest = []
for i, s in enumerate(point_path): for i, s in enumerate(point_path):
@@ -82,7 +80,7 @@ def find_module(current_module, point_path):
raise ModuleNotFound( raise ModuleNotFound(
'The module you searched has not been found') 'The module you searched has not been found')
module_find_path.pop(0) builtin.module_find_path.pop(0)
path = current_namespace[1] path = current_namespace[1]
is_package_directory = current_namespace[2][2] == imp.PKG_DIRECTORY is_package_directory = current_namespace[2][2] == imp.PKG_DIRECTORY

View File

@@ -164,17 +164,17 @@ def gen():
gen_exe = gen() gen_exe = gen()
def dec(func): def nexti(iterator, default=list):
def wrapper(*args, **kwargs): if hasattr("next"):
return func(dict, *args, **kwargs) #return iterator.next()
return wrapper else:
return iterator.__next__()
#return default
@dec
def fu(a, b, c, *args, **kwargs):
return a, b, c, args, kwargs
exe = fu(list, c=set, d='') #from temp import *
#exe[0]. #exe[0].
#exe[4]['d'] #exe[4]['d']
gen_exe. next(gen_exe).

View File

@@ -236,6 +236,8 @@ def gen():
yield 1 yield 1
yield "" yield ""
exe = next(gen()) gen_exe = gen()
#? ['upper'] #? ['upper']
exe.upper next(gen_exe).upper
#? ['real']
next(gen_exe).real

View File

@@ -5,7 +5,8 @@ import re
import StringIO import StringIO
import traceback import traceback
sys.path.append('../') os.chdir('../')
sys.path.append('.')
import functions import functions
#functions.set_debug_function(functions.debug.print_to_stdout) #functions.set_debug_function(functions.debug.print_to_stdout)
@@ -58,10 +59,14 @@ def completion_test(source):
# reset the test, if only one specific test is wanted # reset the test, if only one specific test is wanted
if len(sys.argv) > 2 and line_nr != int(sys.argv[2]): if len(sys.argv) > 2 and line_nr != int(sys.argv[2]):
correct = None correct = None
import debug
debug.debug_function = \
functions.debug.print_to_stdout
debug.ignored_modules = ['parsing', 'builtin']
return tests, fails return tests, fails
# completion tests: # completion tests:
completion_test_dir = 'completion' completion_test_dir = 'test/completion'
summary = [] summary = []
for f_name in os.listdir(completion_test_dir): for f_name in os.listdir(completion_test_dir):
if len(sys.argv) == 1 or [a for a in sys.argv[1:] if a in f_name]: if len(sys.argv) == 1 or [a for a in sys.argv[1:] if a in f_name]: