# Buggy code
def openFileByName(self, fn, gui, old_c, previousSettings):
    """Read the local file whose full path is fn using the given gui.
    fn may be a Leo file (including .leo or zipped file) or an external file.

    This is not a pre-read: the previousSettings always exist and
    the commander created here persists until the user closes the outline.

    Reads the entire outline if fn exists and is a .leo file or zipped file.
    Creates an empty outline if fn is a non-existent Leo file.
    Creates an wrapper outline if fn is an external file, existing or not.
    """
    lm = self
    # Disable the log.
    g.app.setLog(None)
    g.app.lockLog()
    # Create the a commander for the .leo file.
    # Important.  The settings don't matter for pre-reads!
    # For second read, the settings for the file are *exactly* previousSettings.
    c = g.app.newCommander(fileName=fn, gui=gui, previousSettings=previousSettings)
    assert c
    # Open the file, if possible.
    g.doHook("open0")
    theFile = lm.openLeoOrZipFile(fn)
    if isinstance(theFile, sqlite3.Connection):
        # this commander is associated with sqlite db
        c.sqlite_connection = theFile

    # Enable the log.
    g.app.unlockLog()
    c.frame.log.enable(True)
    # Phase 2: Create the outline.
    g.doHook("open1", old_c=None, c=c, new_c=c, fileName=fn)
    if theFile:
        readAtFileNodesFlag = bool(previousSettings)
        # The log is not set properly here.
        ok = lm.readOpenedLeoFile(c, fn, readAtFileNodesFlag, theFile)
        # Call c.fileCommands.openLeoFile to read the .leo file.
        if not ok:
            return None
    else:
        # Create a wrapper .leo file if:
        # a) fn is a .leo file that does not exist or
        # b) fn is an external file, existing or not.
        lm.initWrapperLeoFile(c, fn)
    g.doHook("open2", old_c=None, c=c, new_c=c, fileName=fn)
    # Phase 3: Complete the initialization.
    g.app.writeWaitingLog(c)
    c.setLog()
    lm.createMenu(c, fn)
    lm.finishOpen(c)
    return c


# Traceback (xml.etree.ElementTree.ParseError)
setting leoID from os.getenv('USER'): 'matt'
reading settings in /home/matt/miniconda3/envs/leo/lib/python3.6/site-packages/leo/config/leoSettings.leo
reading settings in /home/matt/.leo/myLeoSettings.leo
QApplication: invalid style override passed, ignoring it.

Leo 5.8 b1, build 20180815095610, Wed, Aug 15, 2018  9:56:10 AM
Not running from a git repo
Python 3.6.6, PyQt version 5.11.1
linux
** isPython3: True

reading settings in /home/matt/.leo/workbook.leo
loadOnePlugin: leo.plugins.livecode.init() returned False
reading settings in /home/matt/code/leo-editor/leo/dist/leoDist.leo
Traceback (most recent call last):

File "/home/matt/miniconda3/envs/leo/lib/python3.6/site-packages/leo/core/leoCommands.py", line 2072, in executeAnyCommand
return command(event)

File "/home/matt/miniconda3/envs/leo/lib/python3.6/site-packages/leo/core/leoGlobals.py", line 269, in commander_command_wrapper
method(event=event)

File "/home/matt/miniconda3/envs/leo/lib/python3.6/site-packages/leo/commands/commanderFileCommands.py", line 214, in open
c2 = g.openWithFileName(fileName, old_c=c)

File "/home/matt/miniconda3/envs/leo/lib/python3.6/site-packages/leo/core/leoGlobals.py", line 3682, in openWithFileName
return g.app.loadManager.loadLocalFile(fileName, gui, old_c)

File "/home/matt/miniconda3/envs/leo/lib/python3.6/site-packages/leo/core/leoApp.py", line 3059, in loadLocalFile
previousSettings = lm.getPreviousSettings(fn)

File "/home/matt/miniconda3/envs/leo/lib/python3.6/site-packages/leo/core/leoApp.py", line 1973, in getPreviousSettings
c = lm.openSettingsFile(fn)

File "/home/matt/miniconda3/envs/leo/lib/python3.6/site-packages/leo/core/leoApp.py", line 2139, in openSettingsFile
readAtFileNodesFlag=False, silent=True)

File "/home/matt/miniconda3/envs/leo/lib/python3.6/site-packages/leo/core/leoFileCommands.py", line 631, in openLeoFile
silent=silent,

File "/home/matt/miniconda3/envs/leo/lib/python3.6/site-packages/leo/core/leoFileCommands.py", line 494, in getLeoFile
v = FastRead(c, self.gnxDict).readFile(fileName)

File "/home/matt/miniconda3/envs/leo/lib/python3.6/site-packages/leo/core/leoFileCommands.py", line 68, in readFile
return self.readWithElementTree(s)

File "/home/matt/miniconda3/envs/leo/lib/python3.6/site-packages/leo/core/leoFileCommands.py", line 73, in readWithElementTree
xroot = ElementTree.fromstring(contents)

File "/home/matt/miniconda3/envs/leo/lib/python3.6/xml/etree/ElementTree.py", line 1314, in XML
parser.feed(text)

File "<string>", line None

xml.etree.ElementTree.ParseError: not well-formed (invalid token): line 189, column 1

# Fixed code
def openFileByName(self, fn, gui, old_c, previousSettings):
    """Read the local file whose full path is fn using the given gui.
    fn may be a Leo file (including .leo or zipped file) or an external file.

    This is not a pre-read: the previousSettings always exist and
    the commander created here persists until the user closes the outline.

    Reads the entire outline if fn exists and is a .leo file or zipped file.
    Creates an empty outline if fn is a non-existent Leo file.
    Creates an wrapper outline if fn is an external file, existing or not.
    """
    lm = self
    # Disable the log.
    g.app.setLog(None)
    g.app.lockLog()
    # Create the a commander for the .leo file.
    # Important.  The settings don't matter for pre-reads!
    # For second read, the settings for the file are *exactly* previousSettings.
    c = g.app.newCommander(fileName=fn, gui=gui, previousSettings=previousSettings)
    # Open the file, if possible.
    g.doHook("open0")
    theFile = lm.openLeoOrZipFile(fn)
    if isinstance(theFile, sqlite3.Connection):
        # this commander is associated with sqlite db
        c.sqlite_connection = theFile
    # Enable the log.
    g.app.unlockLog()
    c.frame.log.enable(True)
    # Phase 2: Create the outline.
    g.doHook("open1", old_c=None, c=c, new_c=c, fileName=fn)
    if theFile:
        readAtFileNodesFlag = bool(previousSettings)
        # The log is not set properly here.
        ok = lm.readOpenedLeoFile(c, fn, readAtFileNodesFlag, theFile)
        # Call c.fileCommands.openLeoFile to read the .leo file.
        if not ok:
            return None
    else:
        # Create a wrapper .leo file if:
        # a) fn is a .leo file that does not exist or
        # b) fn is an external file, existing or not.
        lm.initWrapperLeoFile(c, fn)
    g.doHook("open2", old_c=None, c=c, new_c=c, fileName=fn)
    # Phase 3: Complete the initialization.
    g.app.writeWaitingLog(c)
    c.setLog()
    lm.createMenu(c, fn)
    lm.finishOpen(c)
    return c