# Buggy code
def replace_logical_line(lines, logical, idx, n):
    """Replaces lines at idx that may end in line continuation with a logical
    line that spans n lines.
    """
    if n == 1:
        lines[idx] = logical
        return
    space = " "
    for i in range(idx, idx + n - 1):
        a = len(lines[i])
        b = logical.find(space, a - 1)
        if b < 0:
            # no space found
            lines[i] = logical
            logical = ""
        else:
            # found space to split on
            lines[i] = logical[:b] + "\\"
            logical = logical[b:]
    lines[idx + n - 1] = logical


# Traceback (SyntaxError)
rymg1@DEVPC ~ $ cd abc\
xonsh: To log full traceback to a file set: $XONSH_TRACEBACK_LOGFILE = <filename>
Traceback (most recent call last):
File "C:\Program Files (x86)\Python35\lib\site-packages\xonsh\__amalgam__.py", line 19218, in _parse_ctx_free
debug_level=(self.debug_level > 2))
File "C:\Program Files (x86)\Python35\lib\site-packages\xonsh\parsers\base.py", line 335, in parse
tree = self.parser.parse(input=s, lexer=self.lexer, debug=debug_level)
File "C:\Program Files (x86)\Python35\lib\site-packages\xonsh\ply\ply\yacc.py", line 331, in parse
return self.parseopt_notrack(input, lexer, debug, tracking, tokenfunc)
File "C:\Program Files (x86)\Python35\lib\site-packages\xonsh\ply\ply\yacc.py", line 1199, in parseopt_notrack
tok = call_errorfunc(self.errorfunc, errtoken, self)
File "C:\Program Files (x86)\Python35\lib\site-packages\xonsh\ply\ply\yacc.py", line 193, in call_errorfunc
r = errorfunc(token)
File "C:\Program Files (x86)\Python35\lib\site-packages\xonsh\parsers\base.py", line 2694, in p_error
column=p.lexpos))
File "C:\Program Files (x86)\Python35\lib\site-packages\xonsh\parsers\base.py", line 466, in _parse_error
raise err
File "<string>", line None
SyntaxError: <xonsh-code>:1:2: ('code: abc',)
cd abc\
^

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "C:\Program Files (x86)\Python35\lib\site-packages\xonsh\ptk\shell.py", line 137, in _push
locs=None)
File "C:\Program Files (x86)\Python35\lib\site-packages\xonsh\__amalgam__.py", line 19163, in compile
transform=transform)
File "C:\Program Files (x86)\Python35\lib\site-packages\xonsh\__amalgam__.py", line 19133, in parse
tree, input = self._parse_ctx_free(input, mode=mode, filename=filename)
File "C:\Program Files (x86)\Python35\lib\site-packages\xonsh\__amalgam__.py", line 19236, in _parse_ctx_free
line, nlogical = get_logical_line(lines, idx)
File "C:\Program Files (x86)\Python35\lib\site-packages\xonsh\__amalgam__.py", line 5433, in get_logical_line
line = line[:-1] + lines[idx]
IndexError: list index out of range
rymg1@DEVPC ~ $

# Fixed code
def replace_logical_line(lines, logical, idx, n):
    """Replaces lines at idx that may end in line continuation with a logical
    line that spans n lines.
    """
    if n == 1:
        lines[idx] = logical
        return
    space = " "
    for i in range(idx, idx + n - 1):
        a = len(lines[i])
        b = logical.find(space, a - 1)
        if b < 0:
            # no space found
            lines[i] = logical
            logical = ""
        else:
            # found space to split on
            lines[i] = logical[:b] + str(LINE_CONTINUATION)
            logical = logical[b:]
    lines[idx + n - 1] = logical