# Buggy code
def add_history(self, filename):
    """
    Add new history tab
    Slot for add_history signal emitted by shell instance
    """
    filename = encoding.to_unicode_from_fs(filename)
    if filename in self.filenames:
        return
    editor = codeeditor.CodeEditor(self)
    if osp.splitext(filename)[1] == ".py":
        language = "py"
    else:
        language = "bat"
    editor.setup_editor(
        linenumbers=self.get_option("line_numbers"),
        language=language,
        scrollflagarea=False,
    )
    editor.focus_changed.connect(lambda: self.focus_changed.emit())
    editor.setReadOnly(True)
    color_scheme = self.get_color_scheme()
    editor.set_font(self.get_plugin_font(), color_scheme)
    editor.toggle_wrap_mode(self.get_option("wrap"))

    text, _ = encoding.read(filename)
    text = normalize_eols(text)
    linebreaks = [m.start() for m in re.finditer("\n", text)]
    maxNline = self.get_option("max_entries")
    if len(linebreaks) > maxNline:
        text = text[linebreaks[-maxNline - 1] + 1 :]
        encoding.write(text, filename)
    editor.set_text(text)
    editor.set_cursor_position("eof")

    self.editors.append(editor)
    self.filenames.append(filename)
    index = self.tabwidget.addTab(editor, osp.basename(filename))
    self.find_widget.set_editor(editor)
    self.tabwidget.setTabToolTip(index, filename)
    self.tabwidget.setCurrentIndex(index)


# 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 add_history(self, filename):
    """
    Add new history tab
    Slot for add_history signal emitted by shell instance
    """
    filename = encoding.to_unicode_from_fs(filename)
    if filename in self.filenames:
        return
    editor = codeeditor.CodeEditor(self)
    if osp.splitext(filename)[1] == ".py":
        language = "py"
    else:
        language = "bat"
    editor.setup_editor(
        linenumbers=self.get_option("line_numbers"),
        language=language,
        scrollflagarea=False,
    )
    editor.focus_changed.connect(lambda: self.focus_changed.emit())
    editor.setReadOnly(True)
    color_scheme = self.get_color_scheme()
    editor.set_font(self.get_plugin_font(), color_scheme)
    editor.toggle_wrap_mode(self.get_option("wrap"))

    # Avoid a possible error when reading the history file
    try:
        text, _ = encoding.read(filename)
    except (IOError, OSError):
        text = "# Previous history could not be read from disk, sorry\n\n"
    text = normalize_eols(text)
    linebreaks = [m.start() for m in re.finditer("\n", text)]
    maxNline = self.get_option("max_entries")
    if len(linebreaks) > maxNline:
        text = text[linebreaks[-maxNline - 1] + 1 :]
        # Avoid an error when trying to write the trimmed text to
        # disk.
        # See issue 9093
        try:
            encoding.write(text, filename)
        except (IOError, OSError):
            pass
    editor.set_text(text)
    editor.set_cursor_position("eof")

    self.editors.append(editor)
    self.filenames.append(filename)
    index = self.tabwidget.addTab(editor, osp.basename(filename))
    self.find_widget.set_editor(editor)
    self.tabwidget.setTabToolTip(index, filename)
    self.tabwidget.setCurrentIndex(index)