1.1 --- a/common.py Wed Dec 14 16:40:00 2016 +0100
1.2 +++ b/common.py Wed Dec 14 17:22:07 2016 +0100
1.3 @@ -172,7 +172,7 @@
1.4
1.5 # Constant reference naming.
1.6
1.7 - def get_constant_name(self, value, value_type):
1.8 + def get_constant_name(self, value, value_type, encoding=None):
1.9
1.10 """
1.11 Add a new constant to the current namespace for 'value' with
1.12 @@ -181,7 +181,7 @@
1.13
1.14 path = self.get_namespace_path()
1.15 init_item(self.constants, path, dict)
1.16 - return "$c%d" % add_counter_item(self.constants[path], (value, value_type))
1.17 + return "$c%d" % add_counter_item(self.constants[path], (value, value_type, encoding))
1.18
1.19 # Literal reference naming.
1.20
1.21 @@ -235,27 +235,33 @@
1.22
1.23 def get_constant_value(self, value, literal=None):
1.24
1.25 - "Encode the 'value' if appropriate, returning a value and typename."
1.26 + """
1.27 + Encode the 'value' if appropriate, returning a value, a typename and any
1.28 + encoding.
1.29 + """
1.30
1.31 if isinstance(value, unicode):
1.32 - return value.encode("utf-8"), "unicode"
1.33 + return value.encode("utf-8"), "unicode", self.encoding
1.34
1.35 # Attempt to convert plain strings to text.
1.36
1.37 elif isinstance(value, str) and self.encoding:
1.38 if not literal.startswith("b"):
1.39 try:
1.40 - return unicode(value, self.encoding).encode("utf-8"), "unicode"
1.41 + return unicode(value, self.encoding).encode("utf-8"), "unicode", self.encoding
1.42 except UnicodeDecodeError:
1.43 pass
1.44
1.45 - return value, value.__class__.__name__
1.46 + return value, value.__class__.__name__, None
1.47
1.48 - def get_constant_reference(self, ref, value):
1.49 + def get_constant_reference(self, ref, value, encoding=None):
1.50
1.51 - "Return a constant reference for the given 'ref' type and 'value'."
1.52 + """
1.53 + Return a constant reference for the given 'ref' type and 'value', with
1.54 + the optional 'encoding' applying to text values.
1.55 + """
1.56
1.57 - constant_name = self.get_constant_name(value, ref.get_origin())
1.58 + constant_name = self.get_constant_name(value, ref.get_origin(), encoding)
1.59
1.60 # Return a reference for the constant.
1.61
1.62 @@ -264,25 +270,29 @@
1.63
1.64 # Record the value and type for the constant.
1.65
1.66 - self._reserve_constant(objpath, name_ref.value, name_ref.get_origin())
1.67 + self._reserve_constant(objpath, name_ref.value, name_ref.get_origin(), encoding)
1.68 return name_ref
1.69
1.70 - def reserve_constant(self, objpath, value, origin):
1.71 + def reserve_constant(self, objpath, value, origin, encoding=None):
1.72
1.73 """
1.74 Reserve a constant within 'objpath' with the given 'value' and having a
1.75 - type with the given 'origin'.
1.76 + type with the given 'origin', with the optional 'encoding' applying to
1.77 + text values.
1.78 """
1.79
1.80 constant_name = self.get_constant_name(value, origin)
1.81 objpath = self.get_object_path(constant_name)
1.82 - self._reserve_constant(objpath, value, origin)
1.83 + self._reserve_constant(objpath, value, origin, encoding)
1.84
1.85 - def _reserve_constant(self, objpath, value, origin):
1.86 + def _reserve_constant(self, objpath, value, origin, encoding):
1.87
1.88 - "Store a constant for 'objpath' with the given 'value' and 'origin'."
1.89 + """
1.90 + Store a constant for 'objpath' with the given 'value' and 'origin', with
1.91 + the optional 'encoding' applying to text values.
1.92 + """
1.93
1.94 - self.constant_values[objpath] = value, origin
1.95 + self.constant_values[objpath] = value, origin, encoding
1.96
1.97 def get_literal_reference(self, name, ref, items, cls):
1.98