# Buggy code
def save_entry_slice(entry, source_slice, sha256sum):
    """Save slice of the source file for an entry.

    Args:
        entry: An entry.
        source_slice: The lines that the entry should be replaced with.
        sha256sum: The sha256sum of the current lines of the entry.

    Returns:
        The `sha256sum` of the new lines of the entry.

    Raises:
        FavaAPIException: If the file at `path` is not one of the
            source files.

    """

    with open(entry.meta["filename"], "r") as file:
        lines = file.readlines()

    first_entry_line = entry.meta["lineno"] - 1
    entry_lines = find_entry_lines(lines, first_entry_line)
    entry_source = "".join(entry_lines).rstrip("\n")
    original_sha256sum = sha256(codecs.encode(entry_source)).hexdigest()
    if original_sha256sum != sha256sum:
        raise FavaAPIException("The file changed externally.")

    lines = (
        lines[:first_entry_line]
        + [source_slice + "\n"]
        + lines[first_entry_line + len(entry_lines) :]
    )
    with open(entry.meta["filename"], "w") as file:
        file.writelines(lines)

    return sha256(codecs.encode(source_slice)).hexdigest()


# Traceback (UnicodeDecodeError)
D:\Documents\beancount>c:\Users\belidzs\PycharmProjects\beancount_virtualenv\Scripts\fava --debug fokonyv.beancount
* Serving Flask app "fava.application" (lazy loading)
* Environment: production
WARNING: Do not use the development server in a production environment.
Use a production WSGI server instead.
* Debug mode: on
* Restarting with stat
* Debugger is active!
* Debugger PIN: 546-083-666
* Running on http://localhost:5000/ (Press CTRL+C to quit)
127.0.0.1 - - [09/Mar/2019 10:48:22] "PUT /f%C5%91k%C3%B6nyv/api/add-entries/ HTTP/1.1" 500 -
Traceback (most recent call last):
File "c:\users\belidzs\pycharmprojects\beancount_virtualenv\lib\site-packages\flask\app.py", line 2309, in __call__
return self.wsgi_app(environ, start_response)
File "c:\users\belidzs\pycharmprojects\beancount_virtualenv\lib\site-packages\flask\app.py", line 2295, in wsgi_app
response = self.handle_exception(e)
File "c:\users\belidzs\pycharmprojects\beancount_virtualenv\lib\site-packages\flask\app.py", line 1741, in handle_exception
reraise(exc_type, exc_value, tb)
File "c:\users\belidzs\pycharmprojects\beancount_virtualenv\lib\site-packages\flask\_compat.py", line 35, in reraise
raise value
File "c:\users\belidzs\pycharmprojects\beancount_virtualenv\lib\site-packages\flask\app.py", line 2292, in wsgi_app
response = self.full_dispatch_request()
File "c:\users\belidzs\pycharmprojects\beancount_virtualenv\lib\site-packages\flask\app.py", line 1815, in full_dispatch_request
rv = self.handle_user_exception(e)
File "c:\users\belidzs\pycharmprojects\beancount_virtualenv\lib\site-packages\flask\app.py", line 1718, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "c:\users\belidzs\pycharmprojects\beancount_virtualenv\lib\site-packages\flask\_compat.py", line 35, in reraise
raise value
File "c:\users\belidzs\pycharmprojects\beancount_virtualenv\lib\site-packages\flask\app.py", line 1813, in full_dispatch_request
rv = self.dispatch_request()
File "c:\users\belidzs\pycharmprojects\beancount_virtualenv\lib\site-packages\flask\app.py", line 1799, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "c:\users\belidzs\pycharmprojects\beancount_virtualenv\lib\site-packages\fava\json_api.py", line 42, in _wrapper
return func(request_data)
File "c:\users\belidzs\pycharmprojects\beancount_virtualenv\lib\site-packages\fava\json_api.py", line 26, in _wrapper
json_data = func(*args, **kwargs)
File "c:\users\belidzs\pycharmprojects\beancount_virtualenv\lib\site-packages\fava\json_api.py", line 179, in add_entries
g.ledger.file.insert_entries(entries)
File "c:\users\belidzs\pycharmprojects\beancount_virtualenv\lib\site-packages\fava\core\file.py", line 114, in insert_entries
insert_entry(entry, self.list_sources(), self.ledger.fava_options)
File "c:\users\belidzs\pycharmprojects\beancount_virtualenv\lib\site-packages\fava\core\file.py", line 286, in insert_entry
accounts, entry.date, insert_options, filenames
File "c:\users\belidzs\pycharmprojects\beancount_virtualenv\lib\site-packages\fava\core\file.py", line 328, in find_insert_position
return (filenames[0], len(open(filenames[0]).readlines()) + 1)
File "C:\Program Files\Python36\lib\encodings\cp1250.py", line 23, in decode
return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: 'charmap' codec can't decode byte 0x81 in position 6806: character maps to <undefined>

# Fixed code
def save_entry_slice(entry, source_slice, sha256sum):
    """Save slice of the source file for an entry.

    Args:
        entry: An entry.
        source_slice: The lines that the entry should be replaced with.
        sha256sum: The sha256sum of the current lines of the entry.

    Returns:
        The `sha256sum` of the new lines of the entry.

    Raises:
        FavaAPIException: If the file at `path` is not one of the
            source files.

    """

    with open(entry.meta["filename"], "r", encoding="utf-8") as file:
        lines = file.readlines()

    first_entry_line = entry.meta["lineno"] - 1
    entry_lines = find_entry_lines(lines, first_entry_line)
    entry_source = "".join(entry_lines).rstrip("\n")
    original_sha256sum = sha256(codecs.encode(entry_source)).hexdigest()
    if original_sha256sum != sha256sum:
        raise FavaAPIException("The file changed externally.")

    lines = (
        lines[:first_entry_line]
        + [source_slice + "\n"]
        + lines[first_entry_line + len(entry_lines) :]
    )
    with open(entry.meta["filename"], "w", encoding="utf-8") as file:
        file.writelines(lines)

    return sha256(codecs.encode(source_slice)).hexdigest()