# Buggy code
def writePathChanged(self, p):
    """
    raise IOError if p's path has changed *and* user forbids the write.
    """
    at, c = self, self.c
    #
    # Suppress this message during save-as and save-to commands.
    if c.ignoreChangedPaths:
        return
    oldPath = g.os_path_normcase(at.getPathUa(p))
    newPath = g.os_path_normcase(g.fullPath(c, p))
    try:  # #1367: samefile can throw IOError!
        changed = oldPath and not os.path.samefile(oldPath, newPath)
    except IOError:
        changed = True
    if not changed:
        return
    ok = at.promptForDangerousWrite(
        fileName=None,
        message=(
            f"{g.tr('path changed for %s' % (p.h))}\n{g.tr('write this file anyway?')}"
        ),
    )
    if not ok:
        raise IOError
    at.setPathUa(p, newPath)  # Remember that we have changed paths.


# 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 writePathChanged(self, p):
    """
    raise IOError if p's path has changed *and* user forbids the write.
    """
    at, c = self, self.c
    #
    # Suppress this message during save-as and save-to commands.
    if c.ignoreChangedPaths:
        return
    oldPath = g.os_path_normcase(at.getPathUa(p))
    newPath = g.os_path_normcase(g.fullPath(c, p))
    try:  # #1367: samefile can throw an exception.
        changed = oldPath and not os.path.samefile(oldPath, newPath)
    except Exception:
        changed = True
    if not changed:
        return
    ok = at.promptForDangerousWrite(
        fileName=None,
        message=(
            f"{g.tr('path changed for %s' % (p.h))}\n{g.tr('write this file anyway?')}"
        ),
    )
    if not ok:
        raise IOError
    at.setPathUa(p, newPath)  # Remember that we have changed paths.