# HG changeset patch # User Paul Boddie # Date 1484867385 -3600 # Node ID e678f1913dabede0fa8949a5e0acd22a470c91a1 # Parent c0237ee9fb81de5cd75a5c4bf2aa68db8e5cedb5 Record namespaces with exception handling in order to avoid generating unused variables that provoke warnings when compiling the generated program. diff -r c0237ee9fb81 -r e678f1913dab inspector.py --- a/inspector.py Fri Jan 20 00:08:26 2017 +0100 +++ b/inspector.py Fri Jan 20 00:09:45 2017 +0100 @@ -4,7 +4,7 @@ Inspect and obtain module structure. Copyright (C) 2007, 2008, 2009, 2010, 2011, 2012, 2013, - 2014, 2015, 2016 Paul Boddie + 2014, 2015, 2016, 2017 Paul Boddie This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software @@ -920,6 +920,8 @@ Process the given "try...except" node 'n'. """ + self.record_exception_handler() + tracker = self.trackers[-1] tracker.new_branchpoint() @@ -967,6 +969,8 @@ Process the given "try...finally" node 'n'. """ + self.record_exception_handler() + tracker = self.trackers[-1] self.process_structure_node(n.body) @@ -1492,4 +1496,12 @@ self.function_targets[path][0] -= 1 self.function_arguments[path][0] -= len(args) + 1 + # Exceptions. + + def record_exception_handler(self): + + "Record the current namespace as employing an exception handler." + + self.exception_namespaces.add(self.get_namespace_path()) + # vim: tabstop=4 expandtab shiftwidth=4 diff -r c0237ee9fb81 -r e678f1913dab modules.py --- a/modules.py Fri Jan 20 00:08:26 2017 +0100 +++ b/modules.py Fri Jan 20 00:09:45 2017 +0100 @@ -4,7 +4,7 @@ Module abstractions. Copyright (C) 2007, 2008, 2009, 2010, 2011, 2012, 2013, - 2014, 2015, 2016 Paul Boddie + 2014, 2015, 2016, 2017 Paul Boddie This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software @@ -71,6 +71,10 @@ self.function_targets = {} self.function_arguments = {} + # Exception handler details. + + self.exception_namespaces = set() + # Attribute usage at module and function levels. self.attr_usage = {} @@ -401,6 +405,7 @@ self._get_attr_access_modifiers(f) self._get_constant_literals(f) self._get_constant_values(f) + self._get_exception_namespaces(f) finally: f.close() @@ -633,6 +638,12 @@ self.constant_values[name] = value, value_type, encoding line = f.readline().rstrip() + def _get_exception_namespaces(self, f): + f.readline() # "exception namespaces:" + values = f.readline.rstrip().split(", ") + self.exception_namespaces = set(values) + f.readline() + # Generic parsing methods. def from_lines(self, f, d): @@ -759,10 +770,15 @@ (empty line) "attribute access modifiers:" zero or more: qualified function name " " local/global variable name " " attribute name " " access modifiers + (empty line) "constant literals:" zero or more: qualified scope name " " value type " " constant literal + (empty line) "constant values:" zero or more: qualified name " " value type " " constant literal + (empty line) + "exception namespaces:" + qualified names All collections of names are separated by ", " characters. @@ -995,6 +1011,12 @@ value, value_type, encoding = self.constant_values[name] print >>f, name, value_type, encoding or "{}", repr(value) + print >>f + print >>f, "exception namespaces:" + paths = list(self.exception_namespaces) + paths.sort() + print >>f, ", ".join(paths) + finally: f.close() diff -r c0237ee9fb81 -r e678f1913dab translator.py --- a/translator.py Fri Jan 20 00:08:26 2017 +0100 +++ b/translator.py Fri Jan 20 00:09:45 2017 +0100 @@ -1722,7 +1722,10 @@ self.writeline("__ref __tmp_context, __tmp_value, __tmp_target_value;") self.writeline("__attr %s__tmp_result;" % targets) - self.writeline("__exc __tmp_exc;") + + module = self.importer.get_module(self.name) + if self.get_namespace_path() in module.exception_namespaces: + self.writeline("__exc __tmp_exc;") def write_parameters(self, name):