# Buggy code
def split_buffer(stream, splitter=None, decoder=lambda a: a):
    """Given a generator which yields strings and a splitter function,
    joins all input, splits on the separator and yields each chunk.

    Unlike string.split(), each chunk includes the trailing
    separator, except for the last one if none was found on the end
    of the input.
    """
    splitter = splitter or line_splitter
    buffered = six.text_type("")

    for data in stream_as_text(stream):
        buffered += data
        while True:
            item, rest = splitter(buffered)
            if not item:
                break

            buffered = rest
            yield item

    if buffered:
        yield decoder(buffered)


# Traceback (ValueError)
$ docker-compose -f pep-wilma.yml build pepwilma
Building pepwilma
Step 0 : FROM node:0.10-slim
---> 04e511e59c2e
[...]
Step 16 : ADD https://raw.githubusercontent.com/Bitergia/docker/master/utils/entrypoint-common.sh /
Traceback (most recent call last):
File "/usr/bin/docker-compose", line 9, in <module>
load_entry_point('docker-compose==1.5.1', 'console_scripts', 'docker-compose')()
File "/usr/lib/python2.7/dist-packages/compose/cli/main.py", line 54, in main
command.sys_dispatch()
File "/usr/lib/python2.7/dist-packages/compose/cli/docopt_command.py", line 23, in sys_dispatch
self.dispatch(sys.argv[1:], None)
File "/usr/lib/python2.7/dist-packages/compose/cli/docopt_command.py", line 26, in dispatch
self.perform_command(*self.parse(argv, global_options))
File "/usr/lib/python2.7/dist-packages/compose/cli/main.py", line 171, in perform_command
handler(project, command_options)
File "/usr/lib/python2.7/dist-packages/compose/cli/main.py", line 192, in build
force_rm=bool(options.get('--force-rm', False)))
File "/usr/lib/python2.7/dist-packages/compose/project.py", line 284, in build
service.build(no_cache, pull, force_rm)
File "/usr/lib/python2.7/dist-packages/compose/service.py", line 727, in build
all_events = stream_output(build_output, sys.stdout)
File "/usr/lib/python2.7/dist-packages/compose/progress_stream.py", line 15, in stream_output
for event in utils.json_stream(output):
File "/usr/lib/python2.7/dist-packages/compose/utils.py", line 131, in split_buffer
yield decoder(buffered)
File "/usr/lib/python2.7/json/decoder.py", line 367, in decode
raise ValueError(errmsg("Extra data", s, end, len(s)))
ValueError: Extra data: line 2 column 1 - line 15 column 1 (char 4 - 544)

# Fixed code
def split_buffer(stream, splitter=None, decoder=lambda a: a):
    """Given a generator which yields strings and a splitter function,
    joins all input, splits on the separator and yields each chunk.

    Unlike string.split(), each chunk includes the trailing
    separator, except for the last one if none was found on the end
    of the input.
    """
    splitter = splitter or line_splitter
    buffered = six.text_type("")

    for data in stream_as_text(stream):
        buffered += data
        while True:
            buffer_split = splitter(buffered)
            if buffer_split is None:
                break

            item, buffered = buffer_split
            yield item

    if buffered:
        yield decoder(buffered)