def report(self): self.messages.sort() for pos, is_error, message in self.messages: if is_error: error(pos, message) else: warning(pos, message, 2)
def find_pxd_file(self, qualified_name, pos): # Search include path for the .pxd file corresponding to the # given fully-qualified module name. # Will find either a dotted filename or a file in a # package directory. If a source file position is given, # the directory containing the source file is searched first # for a dotted filename, and its containing package root # directory is searched first for a non-dotted filename. pxd = self.search_include_directories(qualified_name, ".pxd", pos) if pxd is None: # XXX Keep this until Includes/Deprecated is removed if (qualified_name.startswith('python') or qualified_name in ('stdlib', 'stdio', 'stl')): standard_include_path = os.path.abspath(os.path.normpath( os.path.join(os.path.dirname(__file__), os.path.pardir, 'Includes'))) deprecated_include_path = os.path.join(standard_include_path, 'Deprecated') self.include_directories.append(deprecated_include_path) try: pxd = self.search_include_directories(qualified_name, ".pxd", pos) finally: self.include_directories.pop() if pxd: name = qualified_name if name.startswith('python'): warning(pos, "'%s' is deprecated, use 'cpython'" % name, 1) elif name in ('stdlib', 'stdio'): warning(pos, "'%s' is deprecated, use 'libc.%s'" % (name, name), 1) elif name in ('stl'): warning(pos, "'%s' is deprecated, use 'libcpp.*.*'" % name, 1) return pxd
def infer_types(self, scope): enabled = scope.directives['infer_types'] verbose = scope.directives['infer_types.verbose'] if enabled == True: spanning_type = aggressive_spanning_type elif enabled is None: # safe mode spanning_type = safe_spanning_type else: for entry in scope.entries.values(): if entry.type is unspecified_type: entry.type = py_object_type return dependancies_by_entry = {} # entry -> dependancies entries_by_dependancy = {} # dependancy -> entries ready_to_infer = [] for name, entry in scope.entries.items(): if entry.type is unspecified_type: all = set() for expr in entry.assignments: all.update(expr.type_dependencies(scope)) if all: dependancies_by_entry[entry] = all for dep in all: if dep not in entries_by_dependancy: entries_by_dependancy[dep] = set([entry]) else: entries_by_dependancy[dep].add(entry) else: ready_to_infer.append(entry) def resolve_dependancy(dep): if dep in entries_by_dependancy: for entry in entries_by_dependancy[dep]: entry_deps = dependancies_by_entry[entry] entry_deps.remove(dep) if not entry_deps and entry != dep: del dependancies_by_entry[entry] ready_to_infer.append(entry) # Try to infer things in order... while True: while ready_to_infer: entry = ready_to_infer.pop() types = [expr.infer_type(scope) for expr in entry.assignments] if types: entry.type = spanning_type(types) else: # FIXME: raise a warning? # print "No assignments", entry.pos, entry entry.type = py_object_type if verbose: warning(entry.pos, "inferred '%s' to be of type '%s'" % (entry.name, entry.type), 1) resolve_dependancy(entry) # Deal with simple circular dependancies... for entry, deps in dependancies_by_entry.items(): if len(deps) == 1 and deps == set([entry]): types = [expr.infer_type(scope) for expr in entry.assignments if expr.type_dependencies(scope) == ()] if types: entry.type = spanning_type(types) types = [expr.infer_type(scope) for expr in entry.assignments] entry.type = spanning_type(types) # might be wider... resolve_dependancy(entry) del dependancies_by_entry[entry] if ready_to_infer: break if not ready_to_infer: break # We can't figure out the rest with this algorithm, let them be objects. for entry in dependancies_by_entry: entry.type = py_object_type if verbose: warning(entry.pos, "inferred '%s' to be of type '%s' (default)" % (entry.name, entry.type), 1)
def infer_types(self, scope): enabled = scope.directives['infer_types'] verbose = scope.directives['infer_types.verbose'] if enabled == True: spanning_type = aggressive_spanning_type elif enabled is None: # safe mode spanning_type = safe_spanning_type else: for entry in scope.entries.values(): if entry.type is unspecified_type: entry.type = py_object_type return dependancies_by_entry = {} # entry -> dependancies entries_by_dependancy = {} # dependancy -> entries ready_to_infer = [] for name, entry in scope.entries.items(): if entry.type is unspecified_type: all = set() for expr in entry.assignments: all.update(expr.type_dependencies(scope)) if all: dependancies_by_entry[entry] = all for dep in all: if dep not in entries_by_dependancy: entries_by_dependancy[dep] = set([entry]) else: entries_by_dependancy[dep].add(entry) else: ready_to_infer.append(entry) def resolve_dependancy(dep): if dep in entries_by_dependancy: for entry in entries_by_dependancy[dep]: entry_deps = dependancies_by_entry[entry] entry_deps.remove(dep) if not entry_deps and entry != dep: del dependancies_by_entry[entry] ready_to_infer.append(entry) # Try to infer things in order... while True: while ready_to_infer: entry = ready_to_infer.pop() types = [expr.infer_type(scope) for expr in entry.assignments] if types: entry.type = spanning_type(types) else: # FIXME: raise a warning? # print "No assignments", entry.pos, entry entry.type = py_object_type if verbose: warning( entry.pos, "inferred '%s' to be of type '%s'" % (entry.name, entry.type), 1) resolve_dependancy(entry) # Deal with simple circular dependancies... for entry, deps in dependancies_by_entry.items(): if len(deps) == 1 and deps == set([entry]): types = [ expr.infer_type(scope) for expr in entry.assignments if expr.type_dependencies(scope) == () ] if types: entry.type = spanning_type(types) types = [ expr.infer_type(scope) for expr in entry.assignments ] entry.type = spanning_type(types) # might be wider... resolve_dependancy(entry) del dependancies_by_entry[entry] if ready_to_infer: break if not ready_to_infer: break # We can't figure out the rest with this algorithm, let them be objects. for entry in dependancies_by_entry: entry.type = py_object_type if verbose: warning( entry.pos, "inferred '%s' to be of type '%s' (default)" % (entry.name, entry.type), 1)