# Buggy code
def readWithElementTree(self, path, s):
    s = s.translate(None, self.translate_table)
    # Fix #1036 and #1046.
    contents = g.toUnicode(s)
    try:
        xroot = ElementTree.fromstring(contents)
    except Exception as e:
        if path:
            message = f"bad .leo file: {g.shortFileName(path)}"
        else:
            message = "The clipboard is not a vaild .leo file"
        g.es_print("\n" + message, color="red")
        g.es_print(g.toUnicode(e))
        print("")
        # #970: Just report failure here.
        return None
    g_element = xroot.find("globals")
    v_elements = xroot.find("vnodes")
    t_elements = xroot.find("tnodes")
    gnx2body, gnx2ua = self.scanTnodes(t_elements)
    hidden_v = self.scanVnodes(gnx2body, self.gnx2vnode, gnx2ua, v_elements)
    self.handleBits()
    return hidden_v, g_element


# Traceback (TypeError)
bad .leo file: WORK.leo
g.toUnicode: unexpected argument of type ParseError
toUnicode openLeoFile,getLeoFile,readFile,readWithElementTree


Traceback (most recent call last):

File "E:\git\leo\leo\core\leoCommands.py", line 2278, in executeAnyCommand
return command(event)

File "E:\git\leo\leo\core\leoGlobals.py", line 240, in commander_command_wrapper
method(event=event)

File "E:\git\leo\leo\commands\commanderFileCommands.py", line 261, in open_outline
open_completer(c, closeFlag, fileName)

File "E:\git\leo\leo\commands\commanderFileCommands.py", line 204, in open_completer
c2 = g.openWithFileName(fileName, old_c=c)

File "E:\git\leo\leo\core\leoGlobals.py", line 3920, in openWithFileName
return g.app.loadManager.loadLocalFile(fileName, gui, old_c)

File "E:\git\leo\leo\core\leoApp.py", line 3288, in loadLocalFile
previousSettings = lm.getPreviousSettings(fn)

File "E:\git\leo\leo\core\leoApp.py", line 2241, in getPreviousSettings
c = lm.openSettingsFile(fn)

File "E:\git\leo\leo\core\leoApp.py", line 2412, in openSettingsFile
ok = c.fileCommands.openLeoFile(theFile, fn,

File "E:\git\leo\leo\core\leoFileCommands.py", line 692, in openLeoFile
ok, ratio = self.getLeoFile(

File "E:\git\leo\leo\core\leoFileCommands.py", line 559, in getLeoFile
v = FastRead(c, self.gnxDict).readFile(fileName)

File "E:\git\leo\leo\core\leoFileCommands.py", line 60, in readFile
v, g_element = self.readWithElementTree(path, s)

TypeError: cannot unpack non-iterable NoneType object

Traceback (most recent call last):
File "E:\git\leo\leo\plugins\qt_frame.py", line 1638, in closeEvent
g.app.gui.close_event(event)
AttributeError: 'NullGui' object has no attribute 'close_event'

# Fixed code
def readWithElementTree(self, path, s):
    contents = g.toUnicode(s)
    s = contents.translate(self.translate_table)
    # Fix #1036 and #1046.
    try:
        xroot = ElementTree.fromstring(contents)
    except Exception as e:
        # #970: Just report failure here.
        if path:
            message = f"bad .leo file: {g.shortFileName(path)}"
        else:
            message = "The clipboard is not a vaild .leo file"
        g.es_print("\n" + message, color="red")
        g.es_print(g.toUnicode(e))
        print("")
        # #1510: Return a tuple.
        return None, None
    g_element = xroot.find("globals")
    v_elements = xroot.find("vnodes")
    t_elements = xroot.find("tnodes")
    gnx2body, gnx2ua = self.scanTnodes(t_elements)
    hidden_v = self.scanVnodes(gnx2body, self.gnx2vnode, gnx2ua, v_elements)
    self.handleBits()
    return hidden_v, g_element