# Buggy code
def parse(self, lines, comment_tokens=None):
    toplevel = re.compile(r"\S")
    childline = re.compile(r"^\s*(.+)$")

    ancestors = list()
    config = list()

    curlevel = 0
    prevlevel = 0

    for linenum, line in enumerate(str(lines).split("\n")):
        text = str(re.sub(r"([{};])", "", line)).strip()

        cfg = ConfigLine(line)

        if not text or ignore_line(text, comment_tokens):
            continue

        # handle top level commands
        if toplevel.match(line):
            ancestors = [cfg]
            prevlevel = curlevel
            curlevel = 0

        # handle sub level commands
        else:
            match = childline.match(line)
            line_indent = match.start(1)

            prevlevel = curlevel
            curlevel = int(line_indent / self._indent)

            if (curlevel - 1) > prevlevel:
                curlevel = prevlevel + 1

            parent_level = curlevel - 1

            cfg._parents = ancestors[:curlevel]

            if curlevel > len(ancestors):
                config.append(cfg)
                continue

            for i in range(curlevel, len(ancestors)):
                ancestors.pop()

            ancestors.append(cfg)
            ancestors[parent_level].add_child(cfg)

        config.append(cfg)

    return config


# Traceback (UnicodeEncodeError)
The full traceback is:
Traceback (most recent call last):
File "/var/folders/ql/d9_087wj4cd10grvwzxmk4hc0000gn/T/ansible_tcaeDa/ansible_module_iosxr_config.py", line 316, in <module>
main()
File "/var/folders/ql/d9_087wj4cd10grvwzxmk4hc0000gn/T/ansible_tcaeDa/ansible_module_iosxr_config.py", line 310, in main
run(module, result)
File "/var/folders/ql/d9_087wj4cd10grvwzxmk4hc0000gn/T/ansible_tcaeDa/ansible_module_iosxr_config.py", line 235, in run
contents = get_running_config(module)
File "/var/folders/ql/d9_087wj4cd10grvwzxmk4hc0000gn/T/ansible_tcaeDa/ansible_module_iosxr_config.py", line 212, in get_running_config
return NetworkConfig(indent=1, contents=contents)
File "/var/folders/ql/d9_087wj4cd10grvwzxmk4hc0000gn/T/ansible_tcaeDa/ansible_modlib.zip/ansible/module_utils/netcfg.py", line 167, in __init__
File "/var/folders/ql/d9_087wj4cd10grvwzxmk4hc0000gn/T/ansible_tcaeDa/ansible_modlib.zip/ansible/module_utils/netcfg.py", line 200, in load
File "/var/folders/ql/d9_087wj4cd10grvwzxmk4hc0000gn/T/ansible_tcaeDa/ansible_modlib.zip/ansible/module_utils/netcfg.py", line 215, in parse
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe6' in position 1386: ordinal not in range(128)

fatal: [iosxr01]: FAILED! => {
"changed": false,
"failed": true,
"module_stderr": "Traceback (most recent call last):\n  File \"/var/folders/ql/d9_087wj4cd10grvwzxmk4hc0000gn/T/ansible_tcaeDa/ansible_module_iosxr_config.py\", line 316, in <module>\n    main()\n  File \"/var/folders/ql/d9_087wj4cd10grvwzxmk4hc0000gn/T/ansible_tcaeDa/ansible_module_iosxr_config.py\", line 310, in main\n    run(module, result)\n  File \"/var/folders/ql/d9_087wj4cd10grvwzxmk4hc0000gn/T/ansible_tcaeDa/ansible_module_iosxr_config.py\", line 235, in run\n    contents = get_running_config(module)\n  File \"/var/folders/ql/d9_087wj4cd10grvwzxmk4hc0000gn/T/ansible_tcaeDa/ansible_module_iosxr_config.py\", line 212, in get_running_config\n    return NetworkConfig(indent=1, contents=contents)\n  File \"/var/folders/ql/d9_087wj4cd10grvwzxmk4hc0000gn/T/ansible_tcaeDa/ansible_modlib.zip/ansible/module_utils/netcfg.py\", line 167, in __init__\n  File \"/var/folders/ql/d9_087wj4cd10grvwzxmk4hc0000gn/T/ansible_tcaeDa/ansible_modlib.zip/ansible/module_utils/netcfg.py\", line 200, in load\n  File \"/var/folders/ql/d9_087wj4cd10grvwzxmk4hc0000gn/T/ansible_tcaeDa/ansible_modlib.zip/ansible/module_utils/netcfg.py\", line 215, in parse\nUnicodeEncodeError: 'ascii' codec can't encode character u'\\xe6' in position 1386: ordinal not in range(128)\n",
"module_stdout": "",
"msg": "MODULE FAILURE",
"rc": 0
}

# Fixed code
def parse(self, lines, comment_tokens=None):
    toplevel = re.compile(r"\S")
    childline = re.compile(r"^\s*(.+)$")
    entry_reg = re.compile(r"([{};])")

    ancestors = list()
    config = list()

    curlevel = 0
    prevlevel = 0

    for linenum, line in enumerate(
        to_native(lines, errors="surrogate_or_strict").split("\n")
    ):
        text = entry_reg.sub("", line).strip()

        cfg = ConfigLine(line)

        if not text or ignore_line(text, comment_tokens):
            continue

        # handle top level commands
        if toplevel.match(line):
            ancestors = [cfg]
            prevlevel = curlevel
            curlevel = 0

        # handle sub level commands
        else:
            match = childline.match(line)
            line_indent = match.start(1)

            prevlevel = curlevel
            curlevel = int(line_indent / self._indent)

            if (curlevel - 1) > prevlevel:
                curlevel = prevlevel + 1

            parent_level = curlevel - 1

            cfg._parents = ancestors[:curlevel]

            if curlevel > len(ancestors):
                config.append(cfg)
                continue

            for i in range(curlevel, len(ancestors)):
                ancestors.pop()

            ancestors.append(cfg)
            ancestors[parent_level].add_child(cfg)

        config.append(cfg)

    return config