1.1 --- a/micropython/__init__.py Sun Jul 03 20:11:13 2011 +0200
1.2 +++ b/micropython/__init__.py Sun Jul 03 22:51:51 2011 +0200
1.3 @@ -376,7 +376,8 @@
1.4 # Constant records.
1.5
1.6 self.constant_values = {}
1.7 - self.constant_list = None # cache for constants
1.8 + self.constants_used = set()
1.9 + self.constant_references = {}
1.10 self.init_predefined_constants()
1.11
1.12 # Attribute usage.
1.13 @@ -579,6 +580,11 @@
1.14
1.15 self.attribute_users_visited.add(from_name)
1.16
1.17 + # Get constant references.
1.18 +
1.19 + for const in self.constant_references.get(from_name, []):
1.20 + self.constants_used.add(const)
1.21 +
1.22 # The getattr function is a special case: it can potentially reference
1.23 # any known attribute. Since accessor attributes must be known
1.24 # constants, the intersection of known constants and attributes is used
1.25 @@ -709,12 +715,24 @@
1.26
1.27 # Constant accounting.
1.28
1.29 + def use_constant(self, const, from_name):
1.30 +
1.31 + """
1.32 + Register the given 'const' as being used in the program from within an
1.33 + object with the specified 'from_name'.
1.34 + """
1.35 +
1.36 + if not self.constant_references.has_key(from_name):
1.37 + self.constant_references[from_name] = set()
1.38 +
1.39 + self.constant_references[from_name].add(const)
1.40 +
1.41 def init_predefined_constants(self):
1.42
1.43 "Ensure the predefined constants."
1.44
1.45 for name, value in self.predefined_constants.items():
1.46 - self.make_constant(value)
1.47 + self.constants_used.add(self.make_constant(value))
1.48
1.49 def get_predefined_constant(self, name):
1.50
1.51 @@ -750,10 +768,7 @@
1.52
1.53 "Return a list of constants."
1.54
1.55 - if self.constant_list is None:
1.56 - self.constant_list = list(self.constant_values.values())
1.57 -
1.58 - return self.constant_list
1.59 + return self.constants_used
1.60
1.61 # Import methods.
1.62