# Buggy code
def shouldPromptForDangerousWrite(self, fn, p):
    """
    Return True if Leo should warn the user that p is an @<file> node that
    was not read during startup. Writing that file might cause data loss.

    See #50: https://github.com/leo-editor/leo-editor/issues/50
    """
    trace = "save" in g.app.debug
    sfn = g.shortFileName(fn)
    c = self.c
    efc = g.app.externalFilesController
    if p.isAtNoSentFileNode():
        # #1450.
        # No danger of overwriting a file.
        # It was never read.
        return False
    if not g.os_path_exists(fn):
        # No danger of overwriting fn.
        if trace:
            g.trace("Return False: does not exist:", sfn)
        return False
    # #1347: Prompt if the external file is newer.
    if efc:
        # Like c.checkFileTimeStamp.
        if c.sqlite_connection and c.mFileName == fn:
            # sqlite database file is never actually overwriten by Leo,
            # so do *not* check its timestamp.
            pass
        elif efc.has_changed(c, fn):
            if trace:
                g.trace("Return True: changed:", sfn)
            return True
    if hasattr(p.v, "at_read"):
        # Fix bug #50: body text lost switching @file to @auto-rst
        d = p.v.at_read
        for k in d:
            # Fix bug # #1469: make sure k still exists.
            if os.path.exists(k) and os.path.samefile(k, fn) and p.h in d.get(k, set()):
                d[fn] = d[k]
                if trace:
                    g.trace("Return False: in p.v.at_read:", sfn)
                return False
        aSet = d.get(fn, set())
        if trace:
            g.trace(f"Return {p.h not in aSet()}: p.h not in aSet(): {sfn}")
        return p.h not in aSet
    if trace:
        g.trace("Return True: never read:", sfn)
    return True  # The file was never read.


# Traceback (FileNotFoundError)
(base) c:\leo.repo\leo-editor>python c:\leo.repo\leo-editor\launchLeo.py --gui=qttabs --gui=qttabs leo\core\leoPy.leo leo\plugins\leoPlugins.leo

Leo 6.2-b1-devel, devel branch, build 5da736ea4d
2020-02-29 06:19:36 -0600
Unexpected exception reading 'c:/leo.repo/leo-editor/leo/core/leoPy.leo'
Traceback (most recent call last):

File "c:\leo.repo\leo-editor\leo\core\leoApp.py", line 2575, in doPostPluginsInit
c = lm.loadLocalFile(fn, gui=g.app.gui, old_c=None)

File "c:\leo.repo\leo-editor\leo\core\leoApp.py", line 3297, in loadLocalFile
c = lm.openFileByName(fn, gui, old_c, previousSettings)

File "c:\leo.repo\leo-editor\leo\core\leoApp.py", line 3335, in openFileByName
ok = lm.readOpenedLeoFile(c, fn, readAtFileNodesFlag, theFile)

File "c:\leo.repo\leo-editor\leo\core\leoApp.py", line 3505, in readOpenedLeoFile
readAtFileNodesFlag=readAtFileNodesFlag)

File "c:\leo.repo\leo-editor\leo\core\leoFileCommands.py", line 695, in openLeoFile
silent=silent,

File "c:\leo.repo\leo-editor\leo\core\leoFileCommands.py", line 553, in getLeoFile
g.app.checkForOpenFile(c, fileName)

File "c:\leo.repo\leo-editor\leo\core\leoApp.py", line 1476, in checkForOpenFile
if [x for x in aList if os.path.samefile(x, fn)]:

File "c:\leo.repo\leo-editor\leo\core\leoApp.py", line 1476, in <listcomp>
if [x for x in aList if os.path.samefile(x, fn)]:

File "C:\Users\edreamleo\Anaconda3\lib\genericpath.py", line 96, in samefile
s1 = os.stat(f1)

FileNotFoundError: [WinError 2] The system cannot find the file specified: 'C:/Users/edreamleo/Desktop/leo_unittest_position.leo'

Can not create empty workbook
Traceback (most recent call last):

File "c:\leo.repo\leo-editor\leo\core\leoApp.py", line 2602, in doPostPluginsInit
c1 = lm.openEmptyWorkBook()

File "c:\leo.repo\leo-editor\leo\core\leoApp.py", line 2645, in openEmptyWorkBook
c = lm.loadLocalFile(fn, gui=g.app.gui, old_c=None)

File "c:\leo.repo\leo-editor\leo\core\leoApp.py", line 3297, in loadLocalFile
c = lm.openFileByName(fn, gui, old_c, previousSettings)

File "c:\leo.repo\leo-editor\leo\core\leoApp.py", line 3335, in openFileByName
ok = lm.readOpenedLeoFile(c, fn, readAtFileNodesFlag, theFile)

File "c:\leo.repo\leo-editor\leo\core\leoApp.py", line 3505, in readOpenedLeoFile
readAtFileNodesFlag=readAtFileNodesFlag)

File "c:\leo.repo\leo-editor\leo\core\leoFileCommands.py", line 695, in openLeoFile
silent=silent,

File "c:\leo.repo\leo-editor\leo\core\leoFileCommands.py", line 553, in getLeoFile
g.app.checkForOpenFile(c, fileName)

File "c:\leo.repo\leo-editor\leo\core\leoApp.py", line 1476, in checkForOpenFile
if [x for x in aList if os.path.samefile(x, fn)]:

File "c:\leo.repo\leo-editor\leo\core\leoApp.py", line 1476, in <listcomp>
if [x for x in aList if os.path.samefile(x, fn)]:

File "C:\Users\edreamleo\Anaconda3\lib\genericpath.py", line 96, in samefile
s1 = os.stat(f1)

FileNotFoundError: [WinError 2] The system cannot find the file specified: 'C:/Users/edreamleo/Desktop/leo_unittest_position.leo'

# Fixed code
def shouldPromptForDangerousWrite(self, fn, p):
    """
    Return True if Leo should warn the user that p is an @<file> node that
    was not read during startup. Writing that file might cause data loss.

    See #50: https://github.com/leo-editor/leo-editor/issues/50
    """
    trace = "save" in g.app.debug
    sfn = g.shortFileName(fn)
    c = self.c
    efc = g.app.externalFilesController
    if p.isAtNoSentFileNode():
        # #1450.
        # No danger of overwriting a file.
        # It was never read.
        return False
    if not g.os_path_exists(fn):
        # No danger of overwriting fn.
        if trace:
            g.trace("Return False: does not exist:", sfn)
        return False
    # #1347: Prompt if the external file is newer.
    if efc:
        # Like c.checkFileTimeStamp.
        if c.sqlite_connection and c.mFileName == fn:
            # sqlite database file is never actually overwriten by Leo,
            # so do *not* check its timestamp.
            pass
        elif efc.has_changed(c, fn):
            if trace:
                g.trace("Return True: changed:", sfn)
            return True
    if hasattr(p.v, "at_read"):
        # Fix bug #50: body text lost switching @file to @auto-rst
        d = p.v.at_read
        for k in d:
            # Fix bug # #1469: make sure k still exists.
            if os.path.exists(k) and os.path.samefile(k, fn) and p.h in d.get(k, set()):
                d[fn] = d[k]
                if trace:
                    g.trace("Return False: in p.v.at_read:", sfn)
                return False
        aSet = d.get(fn, set())
        if trace:
            g.trace(f"Return {p.h not in aSet()}: p.h not in aSet(): {sfn}")
        return p.h not in aSet
    if trace:
        g.trace("Return True: never read:", sfn)
    return True  # The file was never read.