# Buggy code
def symlink(value, filename):  # analysis:ignore
    newlinkname = filename + "." + unique() + ".newlink"
    newvalname = os.path.join(newlinkname, "symlink")
    os.mkdir(newlinkname)
    f = _open(newvalname, "wb")
    f.write(to_binary_string(value))
    f.flush()
    f.close()
    try:
        rename(newlinkname, filename)
    except:
        try:
            os.remove(newvalname)
            os.rmdir(newlinkname)
        except (IOError, OSError):
            pass
        raise


# Traceback (FileExistsError)
File "D:\Anaconda\envs\Mau\lib\site-packages\spyder\utils\external\lockfile.py", line 162, in lock
symlink(str(os.getpid()), self.name)
File "D:\Anaconda\envs\Mau\lib\site-packages\spyder\utils\external\lockfile.py", line 97, in symlink
rename(newlinkname, filename)
FileExistsError: [WinError 183] Cannot create a file when that file already exists: 'C:\\Users\\sdesai\\.spyder-py3\\spyder.lock.1554264487277.newlink' -> 'C:\\Users\\sdesai\\.spyder-py3\\spyder.lock'

Traceback (most recent call last):
File "D:\Anaconda\envs\Mau\lib\site-packages\spyder\utils\external\lockfile.py", line 189, in lock
kill(int(pid), 0)
File "D:\Anaconda\envs\Mau\lib\site-packages\spyder\utils\external\lockfile.py", line 81, in kill
raise OSError(errno.ESRCH, None)
ProcessLookupError: [Errno 3] None

Traceback (most recent call last):
File "D:\Anaconda\envs\Mau\lib\site-packages\spyder\app\start.py", line 157, in main
lock_created = lock.lock()
File "D:\Anaconda\envs\Mau\lib\site-packages\spyder\utils\external\lockfile.py", line 212, in lock
rmlink(self.name)
File "D:\Anaconda\envs\Mau\lib\site-packages\spyder\utils\external\lockfile.py", line 119, in rmlink
os.remove(os.path.join(filename, 'symlink'))
PermissionError: [WinError 5] Access is denied: 'C:\\Users\\sdesai\\.spyder-py3\\spyder.lock\\symlink'

Traceback (most recent call last):
File "D:\Anaconda\envs\Mau\lib\site-packages\spyder\plugins\__init__.py", line 515, in <lambda>
toggled=lambda checked: self.toggle_view(checked),
File "D:\Anaconda\envs\Mau\lib\site-packages\spyder\plugins\ipythonconsole.py", line 771, in toggle_view
self.create_new_client(give_focus=False)
File "D:\Anaconda\envs\Mau\lib\site-packages\spyder\plugins\ipythonconsole.py", line 1111, in create_new_client
self.register_client(client)
File "D:\Anaconda\envs\Mau\lib\site-packages\spyder\plugins\ipythonconsole.py", line 1307, in register_client
self.main.historylog.add_history(client.history_filename)
File "D:\Anaconda\envs\Mau\lib\site-packages\spyder\plugins\history.py", line 240, in add_history
encoding.write(text, filename)
File "D:\Anaconda\envs\Mau\lib\site-packages\spyder\utils\encoding.py", line 233, in write
with open(filename, mode) as textfile:
PermissionError: [Errno 13] Permission denied: 'C:\\Users\\sdesai\\.spyder-py3\\history.py'

# Fixed code
def symlink(value, filename):  # analysis:ignore
    newlinkname = filename + "." + unique() + ".newlink"
    newvalname = os.path.join(newlinkname, "symlink")
    os.mkdir(newlinkname)
    f = _open(newvalname, "wb")
    f.write(to_binary_string(value))
    f.flush()
    f.close()
    try:
        rename(newlinkname, filename)
    except:
        # This is needed to avoid an error when we don't
        # have permissions to write in ~/.spyder
        # See issues 6319 and 9093
        try:
            os.remove(newvalname)
            os.rmdir(newlinkname)
        except (IOError, OSError):
            return
        raise