# Buggy code
def substitute(self, mapping):
    # Helper function for .sub()

    def convert(mo):
        named = mo.group("named") or mo.group("braced")
        braced = mo.group("braced")
        if braced is not None:
            sep = mo.group("sep")
            result = self.process_braced_group(braced, sep, mapping)
            if result:
                return result

        if named is not None:
            val = mapping[named]
            return "%s" % (val,)
        if mo.group("escaped") is not None:
            return self.delimiter
        if mo.group("invalid") is not None:
            self._invalid(mo)
        raise ValueError("Unrecognized named group in pattern", self.pattern)

    return self.pattern.sub(convert, self.template)


# Traceback (UnicodeDecodeError)
Traceback (most recent call last):
File "/usr/bin/docker-compose", line 11, in <module>
sys.exit(main())
File "/usr/lib/python2.7/site-packages/compose/cli/main.py", line 71, in main
command()
File "/usr/lib/python2.7/site-packages/compose/cli/main.py", line 118, in perform_command
handler(command, options, command_options)
File "/usr/lib/python2.7/site-packages/compose/cli/main.py", line 322, in config
print(serialize_config(compose_config, image_digests))
File "/usr/lib/python2.7/site-packages/compose/config/serialize.py", line 93, in serialize_config
width=80
File "/usr/lib/python2.7/site-packages/yaml/__init__.py", line 218, in safe_dump
return dump_all([data], stream, Dumper=SafeDumper, **kwds)
File "/usr/lib/python2.7/site-packages/yaml/__init__.py", line 190, in dump_all
dumper.represent(data)
File "/usr/lib/python2.7/site-packages/yaml/representer.py", line 28, in represent
node = self.represent_data(data)
File "/usr/lib/python2.7/site-packages/yaml/representer.py", line 57, in represent_data
node = self.yaml_representers[data_types[0]](self, data)
File "/usr/lib/python2.7/site-packages/yaml/representer.py", line 225, in represent_dict
return self.represent_mapping(u'tag:yaml.org,2002:map', data)
File "/usr/lib/python2.7/site-packages/yaml/representer.py", line 123, in represent_mapping
node_value = self.represent_data(item_value)
File "/usr/lib/python2.7/site-packages/yaml/representer.py", line 57, in represent_data
node = self.yaml_representers[data_types[0]](self, data)
File "/usr/lib/python2.7/site-packages/yaml/representer.py", line 225, in represent_dict
return self.represent_mapping(u'tag:yaml.org,2002:map', data)
File "/usr/lib/python2.7/site-packages/yaml/representer.py", line 123, in represent_mapping
node_value = self.represent_data(item_value)
File "/usr/lib/python2.7/site-packages/yaml/representer.py", line 57, in represent_data
node = self.yaml_representers[data_types[0]](self, data)
File "/usr/lib/python2.7/site-packages/yaml/representer.py", line 225, in represent_dict
return self.represent_mapping(u'tag:yaml.org,2002:map', data)
File "/usr/lib/python2.7/site-packages/yaml/representer.py", line 123, in represent_mapping
node_value = self.represent_data(item_value)
File "/usr/lib/python2.7/site-packages/yaml/representer.py", line 57, in represent_data
node = self.yaml_representers[data_types[0]](self, data)
File "/usr/lib/python2.7/site-packages/yaml/representer.py", line 225, in represent_dict
return self.represent_mapping(u'tag:yaml.org,2002:map', data)
File "/usr/lib/python2.7/site-packages/yaml/representer.py", line 123, in represent_mapping
node_value = self.represent_data(item_value)
File "/usr/lib/python2.7/site-packages/yaml/representer.py", line 57, in represent_data
node = self.yaml_representers[data_types[0]](self, data)
File "/usr/lib/python2.7/site-packages/compose/config/serialize.py", line 30, in serialize_string
data = data.replace('$', '$$')
UnicodeDecodeError: ‘ascii’ codec can't decode byte 0xe2 in position 1: ordinal not in range(128)

# Fixed code
def substitute(self, mapping):
    # Helper function for .sub()

    def convert(mo):
        named = mo.group("named") or mo.group("braced")
        braced = mo.group("braced")
        if braced is not None:
            sep = mo.group("sep")
            result = self.process_braced_group(braced, sep, mapping)
            if result:
                return result

        if named is not None:
            val = mapping[named]
            if isinstance(val, six.binary_type):
                val = val.decode("utf-8")
            return "%s" % (val,)
        if mo.group("escaped") is not None:
            return self.delimiter
        if mo.group("invalid") is not None:
            self._invalid(mo)
        raise ValueError("Unrecognized named group in pattern", self.pattern)

    return self.pattern.sub(convert, self.template)